OFPEC Forum
Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: RujiK on 20 Feb 2005, 02:27:06
-
Cos, Sin, atan, atan2
I Cannot get these commands to do as I wish them to!
I simply wish to make an object sit at the LEFT or right of your user no matter what direction you are facing.
I can only get the object to sit in front or behind your user, I cannot get them on the side.
I am not yet old enough to take Geomotry so this is all knew.
Any help would be greatly appreciated.
-
FDF uses this to deploy their tripods
_w setpos[(getpos Player select 0)+((sin getdir Player)*2), (getpos Player select 1)+((cos getdir Player)*2), 0]
a position is an array and is made up of either 2 or 3 elements
eg
x axis
y axis
height
all you need to do is offset the position of the object to the player after getting the players direction
getdir = gets direction player is facing ( 0 to 359 degrees from north)
(getpos Player select 0)+((sin getdir Player)*2)
sets the x axis position
etc etc
to find out how things work, grab a copy of a de pbo program and decompress scripts.pbo or mission pbo's, thats the easiest way to find out how things are done
-
Here are some formulae I use:
To convert a relative position into a set of x,y coordinates
(for camera positioning)
if:
x1,y1 are the coordinates of the object
theta is the direction of the object
a,b are the relative position parameters
then
X coordinate = x1 + b*sin(theta) + a*cos(theta)
y coordinate = y1 + b*cos(theta) - a*sin(theta)
__________________________________________________________________________
To find the coordinates of a point in front of or to the side of an object
(these are sub-sets of the formula above)
if:
x1,y1 are the coordinates of the object
theta is the direction of the object
d is the distance of the point from the object
then:
For a point in front of the object
X coordinate = x1 + d*sin(theta)
y coordinate = y1 + d*cos(theta)
(for a point behind use -d or theta + 180
For a point to the right of the object
X coordinate = x1 + d*cos(theta)
y coordinate = y1 - d*sin(theta)
(for a point to the left use -d or theta + 180)
__________________________________________________________________________
To rotate a point about another point.
This is useful if you select a random point in
a rectangular area, but you want the rectangle to be at an angle, not just north-south, east-west.
if:
x1,y1 are the coordinates for the point about which the rotation takes place
x2,y2 are the coordinates of the original location of the point to be rotated
theta is the angle of rotation
The coordinates of the rotated point are:
x = x1 + (x2-x1)*Cos(theta) - (y2-y1)*Sin(theta)
y = y1 + (y2-y1)*Cos(theta) + (x2-x1)*Sin(theta)
-
Thats a GREAT idea! Thanks Thobson!
I never would have thought to add the dir of my unit!
(Also to Terox that is what I was doing origanally but thanks anyway.)
-
? (!Like trig): Goto "hereendeththelesson"
The following creates the absolute grid coordinates of a point from polar coordinates (distance + angle) relative to the position and direction of the origin object. It is, of course, related to THobson's:
X coordinate = x1 + d*sin(theta)
y coordinate = y1 + d*cos(theta)
but does away with the need to think about and change the formulae for the different directions. The trick is to add the target's direction to the origin's heading, so our formulae become:
x coordinate = x1 + d*sin(omega + theta)
y coordinate = y1 + d*cos(omega + theta)
Where omega can be considered the origin's facing and theta the relative direction of the target position. Logically, if I'm facing East (+90 degrees) and want an object on my left (+270 degrees from MY heading), the target object wants to be North of my position (360 = 90 + 270):
#hereendeththelesson
For trigonometrophobes, here's the script form:
_targetx = (GetPos _origin) Select 0 + (_distance * Sin (GetDir _origin + _reldir))
_targety = (GetPos _origin) Select 1 + (_distance * Cos (GetDir _origin + _reldir))
_targetpos = [_targetx,_targety,0]
_origin = your starting unit/object
_distance = the distance to the target position
_reldir = the relative direction of target position in degrees FROM THE DIRECTION _origin IS FACING, e.g.
Front: _reldir = 0
Right: _reldir = 90
Rear: _reldir = 180
Left: _reldir = 270
Half right: _reldir = 45
. . . and so on.
-
Well thanks for the help however I found a way to make an object to the left (or right) of you, after all that complex scripting I found a simple way ;D
Here it is:
_obj = _this select 0
_xdefault = getpos player select 0
_ydefault = getpos player select 1
_x = _xdefault-((cos getdir player)*1.2)
_y = _ydefault+((sin getdir player)*1.2)
_obj setpos [_x,_y,0]
Yep, thats it very easy, all you have to do is subtract the cos and add the sin, or vise versa.