Jump to content
Sign in to follow this  
Serpent

Define a relative position to objects

Recommended Posts

Privjet

Is there a possibility in OFP to define a position relativly to an object?

Situation:

A player (let's call him "pl1") got by a script the option to build up a certain object (let's take an M2-MG named "m2mg"). This object should get placed _xy meters in front of the player no matter what's his angle of sight.

My script so far:

_r= (getdir player)+0

standort setpos [(getpos player select 0)+(sin _r),(getpos player select 1)+(sin _r),0];

_m2mg="M2StaticMG" createvehicle (getpos standort);

exit;

As far as I see the "_m2mg" gets placed in a relative position to the main grid but not to the player. Am I right there? What is the mathematic formula to place the object right in front of the player? Can someone make an example for a player's angle of sight of _deg degrees? The object _m2mg should be placed always in front of the player no matter how much _deg is.

Share this post


Link to post
Share on other sites

Let the player's direction be _dir, and let the distance of the object to be placed in front of the player be _dist.

_dx = cos(_dir) * _dist + sin(_dir) * _dist

_dy = -sin(_dir) * _dist + cos(_dir) * _dist

Now, add these onto the X/Y position of the player.

Prospero

Share this post


Link to post
Share on other sites

hm, it doesn't work. The gun always appears on the right side of the player, guess in an angle of 90°...

//edit

now it works but in a very strange way. Here's my script:

_dir= (getdir player)+315

_dx = cos(_dir) * 2 + sin(_dir) * 2

_dy = -sin(_dir) * 2 + cos(_dir) * 2

standort setpos [(getpos player select 0)+_dx,(getpos player select 1)+_dy,0];

_mg="M2StaticMG" createvehicle (getpos standort);

_mg setdir (getdir player);

exit;

As long as I add +315° to +325° the gun appears in front of the player. Does anyone find the mistake in the script?

Share this post


Link to post
Share on other sites

I didn't look at your script, but be aware that orientation and angle is different for objects and sin/cos functions.  For objects, facing North is 0 degrees and we count clockwise.  East would be 90 degrees for example.  For the sine and cosine stuff, 0 degrees is East and we count counter-clockwise, so 90 degrees would be North, etc.

Here's how I change an "angle function" to an object's direction:

_x = _pos select 0

_z = _pos select 1

_dir = _z atan2 _x

_obj setDir (90 - _dir)

Doolittle

Share this post


Link to post
Share on other sites

I am doing much the same thing in a script and what I'm doing is along the lines of-

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

_dir= (getdir player)

_dx = _dist * sin _dir

_dy = _dist * cos _dir

standort setpos [(getpos player select 0)+_dx,(getpos player select 1)+_dy,0];

_mg="M2StaticMG" createvehicle (getpos standort);

_mg setdir (getdir player);

exit;<span id='postcolor'>

Share this post


Link to post
Share on other sites

Hi. Could anyone suggest a simple script that would check a relative position between two points? The aim is just to check the angle between the vectors of two randomly generated points so that they are not situated on a more or less straight line vertically or horizontally. In the readme of the ECP mod, it is said:

 

"[<unit>,<position>] call (ECP_resources select 4)
ECP_getDirRelPos returns the relative direction in degrees from <unit> to <position>. 0 degrees
is considered to be to the direct front of <unit>, 90 is to the right etc."

 

But I can't find even the place where this array, ECP_recources, is announced, not to say the script itself.
 

Share this post


Link to post
Share on other sites
7 hours ago, zwobot said:

 

The problem is that there are just two sets of coordinates, not objects. So the first script is irrelevant because it uses getdir, and the second – because it does not return angle and uses _northdirection that should somehow be known.

Share this post


Link to post
Share on other sites

So why don't you write a custom function yourself then? It seems all you need is a dot product of two vectors?

Share this post


Link to post
Share on other sites
18 hours ago, zwobot said:

It seems all you need is a dot product of two vectors? 

The direction matters. I cannot just take an arbitrary third point to define two vectors.

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  

×