Jump to content
Sign in to follow this  
Axo

Nearestobject to any object

Recommended Posts

I´m trying to get the closest object (of any kind) to an object but I can´t figure how...

I´ve tryed:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Near = nearestobject [_Obj,""]<span id='postcolor'>

And

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Near = nearestobject [_Obj,"All"]<span id='postcolor'>

etc...

If can be done with vehicles, works for me smile.gif

Another option is to repeat the line for every vehicle...

PD: Is there a way to know all the vehicles object currently in a map (w/o triggers)

Share this post


Link to post
Share on other sites

That should work if you have initialised the variable _Obj with an object name.

Bear in mind that the nearestobject command will only work for a certain distance.

RED

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (RED @ Jan. 31 2003,07:11)</td></tr><tr><td id="QUOTE">That should work if you have initialised the variable _Obj with an object name.

Bear in mind that the nearestobject command will only work for a certain distance.

RED<span id='postcolor'>

Of course _Obj must be initialised.

It doesn´t, the first sample returns object _Obj and the second never returns anything...:(

Share this post


Link to post
Share on other sites

I would like to know this also, it would make it much easier to track bullets with a camera if you dont have to check for the bulletnames.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Axo @ Jan. 31 2003,03:15)</td></tr><tr><td id="QUOTE">I´m trying to get the closest object (of any kind) to an object but I can´t figure how...

I´ve tryed:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Near = nearestobject [_Obj,""]<span id='postcolor'>

And

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_Near = nearestobject [_Obj,"All"]<span id='postcolor'>

etc...

If can be done with vehicles, works for me smile.gif

Another option is to repeat the line for every vehicle...

PD: Is there a way to know all the vehicles object currently in a map (w/o triggers)<span id='postcolor'>

Afaik it's not possible.

Share this post


Link to post
Share on other sites

Is there a way to know all the vehicles object currently in a map (w/o triggers cause is for an addon script)???

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">Is there a way to know all the vehicles object currently in a map (w/o triggers cause is for an addon script)???<span id='postcolor'>

Er....I really did not understand the question 100% smile.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (InqWiper @ Jan. 31 2003,18:58)</td></tr><tr><td id="QUOTE">I would like to know this also, it would make it much easier to track bullets with a camera if you dont have to check for the bulletnames.<span id='postcolor'>

NearestObject will catch the nearest object of ANY type when you leave out the "". That is,

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_obj = nearestObject [getpos player];<span id='postcolor'>

works OK. The problem is, that the nearest ANY object from the player is always the player itself.

To catch any object in front of the player, you must not specify the point of reference to be the player itself, but some point a bit ahead of the player. Something like this:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_obj = nearestObject [((getpos player select 0) + 3*(sin getDir player)), ((getpos player select 1) + 3*(cos getdir player)), getpos player select 2];<span id='postcolor'>

The previous will check the nearest object from a point which is 3m ahead of the player, regardless the heading of the player. If the player is the closest object to that spot, the _obj will point to the player. Therefore you have to check if the _obj is of class "Man" or something else.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">?("Man" countType [_obj] == 0) : hint "_obj is not the player";<span id='postcolor'>

Unfortunately, a bullet doesn't seem to belong in any "type" that you can define in countType. If it did, it would be easy to catch a fired bullet.

So far the only solution would be to use all the appropriate bullet names in nearestObject itself.

EDIT: I just came up with an idea... The script will have to check the velocity of each object it finds... The object has to be a bullet if the velocity is high enough. I'll do some experiments with this and post any findings.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (InqWiper @ Jan. 31 2003,15:22)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">Is there a way to know all the vehicles object currently in a map (w/o triggers cause is for an addon script)???<span id='postcolor'>

Er....I really did not understand the question 100%  smile.gif<span id='postcolor'>

Any way of building an array of all the vehicles in a mission dynamicaly from a script (not by hand)

This script it´s going to be inside an addon, so it´s no way to know what vehicles will be using the mission.

I´ll need the vehicles list to ask for nearestobject[_Obj,"vehicle"] for each one.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Spitfire @ Jan. 31 2003,15:32)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (InqWiper @ Jan. 31 2003,18:58)</td></tr><tr><td id="QUOTE">I would like to know this also, it would make it much easier to track bullets with a camera if you dont have to check for the bulletnames.<span id='postcolor'>

NearestObject will catch the nearest object of ANY type when you leave out the "". That is,

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_obj = nearestObject [getpos player];<span id='postcolor'>

works OK. The problem is, that the nearest ANY object from the player is always the player itself.

To catch any object in front of the player, you must not specify the point of reference to be the player itself, but some point a bit ahead of the player. Something like this:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_obj = nearestObject [((getpos player select 0) + 3*(sin getDir player)), ((getpos player select 1) + 3*(cos getdir player)), getpos player select 2];<span id='postcolor'>

The previous will check the nearest object from a point which is 3m ahead of the player, regardless the heading of the player. If the player is the closest object to that spot, the _obj will point to the player. Therefore you have to check if the _obj is of class "Man" or something else.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">?("Man" countType [_obj] == 0) : hint "_obj is not the player";<span id='postcolor'>

Unfortunately, a bullet doesn't seem to belong in any "type" that you can define in countType. If it did, it would be easy to catch a fired bullet.

So far the only solution would be to use all the appropriate bullet names in nearestObject itself.<span id='postcolor'>

Any idea of how to get a point ahead (20 mts) of a flying object (it must be in 3D)

I´ve tryed this...

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_vm = velocity _Obj

_vx= _vm select 0

_vy= _vm select 1

_vz= _vm select 2

_vmod = sqrt( _vx^2+ _vx^2+ _vx^2)

_pm = position _Obj

_px= _pm select 0 + (_vx*20/_vmod)

_py= _pm select 1 + (_vy*20/_vmod)

_pz= _pm select 2 + (_vz*20/_vmod)

<span id='postcolor'>

for some reason this doesn´t works...It gives me an error of "divide by zero" even when i´m sure the object is moving!

Share this post


Link to post
Share on other sites

To me it doesnt seem like nearestobject would be a good way to catch all vehicles in a list or something because then you would have to check nearestobject from every centimeter of the map, atleast it seems like it to me.

I dont have any good idea how to do this, it would be nice if you could just camcreate a trigger tounge.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">Any idea of how to get a point ahead (20 mts) of a flying object (it must be in 3D)

<span id='postcolor'>

Kam="camera" camcreate getpos [0,0,0]

Kam camsettarget Chopper

Kam camsettrelpos [0,20,0]

Kam camcommit 0

Pos=getpos Kam

I think that should work.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (InqWiper @ Jan. 31 2003,15:54)</td></tr><tr><td id="QUOTE">To me it doesnt seem like nearestobject would be a good way to catch all vehicles in a list or something because then you would have to check nearestobject from every centimeter of the map, atleast it seems like it to me.

I dont have any good idea how to do this, it would be nice if you could just camcreate a trigger  tounge.gif<span id='postcolor'>

I´d be very nice to dynamically add triggers.... Suma... wink.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (InqWiper @ Jan. 31 2003,15:57)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">Any idea of how to get a point ahead (20 mts) of a flying object (it must be in 3D)

<span id='postcolor'>

Kam="camera" camcreate getpos [0,0,0]

Kam camsettarget Chopper

Kam camsettrelpos [0,20,0]

Kam camcommit 0

Pos=getpos Kam

I think that should work.<span id='postcolor'>

But then I´ll have an object in that position, and I won´t be able to use nearestobject there, right?

the camera object dissapears if I do a camdestroy?

Share this post


Link to post
Share on other sites

None of those options will work, since he needs it to be in 3d, meaning that if the object is flying upwards, the point will have be higher.

It can be done by checking the true velocity (in 3d) of the object, calculating the angle between the velocity vector and the horizon. With that information you can calculate the point in 3d using simple Pythagoras.

Share this post


Link to post
Share on other sites

Darn, of course I should've come up with this...

You can catch any bulled fired by the player by using the "fired" eventhandler. The fifth parameter of the eventhandler is always the ammo name.

the init field of the player:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">this addEventHandler ["fired", {_this exec "fired.sqs"}]<span id='postcolor'>

fired.sqs:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">_ammoname = _this select 4;

_obj = nearestObject [getpos player, _ammoname];

hint format ["Bullet: %1\nBullet speed:%2",_ammoname, speed _obj];<span id='postcolor'>

The script will grab the bullet (or grenade) fired by the player and display it's speed.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">None of those options will work, since he needs it to be in 3d, meaning that if the object is flying upwards, the point will have be higher.

<span id='postcolor'>

Camsetrelpos [0,20,0] means 20 meters infront of the object no matter what the altitude is.

I didnt know he wanted to check for bullets or I could have told him the eventhandler would be better. We had a thread about this not long time ago.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">But then I´ll have an object in that position, and I won´t be able to use nearestobject there, right?

the camera object dissapears if I do a camdestroy? <span id='postcolor'>

I just said how to get a point 20 meters infront of an object, never said anything about what you should do to make a camera track a fired bullet or something as far as I can remember.

I see no reason why you should not be able to use nearestobject from that position.

The camera will be destroyed if you use camdestroy Kam

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (InqWiper @ Feb. 01 2003,14:39)</td></tr><tr><td id="QUOTE">Camsetrelpos [0,20,0] means 20 meters infront of the object no matter what the altitude is.<span id='postcolor'>

I didn't mean the altitude, I meant the attitude.

If the nose of the aircraft is pointing upwards, the point 20m ahead of the plane is higher than the plane. So only by getting the velocity vector and calculating the nose-up attitude from there gives the correct results.

EDIT: Of course, the pitch calculation will be only a rough estimate, since aircraft doesn't necessarily move to the same direction where its nose is pointing at. You can only calculate the point along the movement axis.

Share this post


Link to post
Share on other sites

The camera way does consider attitude, but unfortunately you can´t get rid of the object with camdestroy...

In one of my early posts I tryed the vector method unsuccessfully, but today I made it work (it was a brackets thing):</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_vm = velocity _Obj

_vx= _vm select 0

_vy= _vm select 1

_vz= _vm select 2

_vmod = sqrt( _vx^2+ _vx^2+ _vx^2)

_pm = position _Obj

_px= (_pm select 0) + ((_vx*20)/_vmod)

_py= (_pm select 1) + ((_vy*20)/_vmod)

_pz= (_pm select 2) + ((_vz*20)/_vmod)

#_px, _py, _pz : point 20 mts ahead of _Obj

<span id='postcolor'>

Thanks for the tips!! smile.gif

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×