Home   Help Search Login Register  

Author Topic: Relative Position account for Direction  (Read 1325 times)

0 Members and 1 Guest are viewing this topic.

Offline Doolittle

  • Contributing Member
  • **
  • _this select 0
Relative Position account for Direction
« on: 04 Oct 2005, 21:17:24 »
I'm trying to figure out how to.... given object A and object B... I create object C anywhere, and I'd like to place and object D that is in relation to C... the same as B is in relation to A.  This needs to take into account the direction of A, B, C, and D.

If YOU were object A, and your computer was object B, I'd like to be able to place object C in the street near you, facing any random direction, and then I want to put down object D near C that is the same distance from C as B is to A, and if your computer (B) is in front of you (A) and to the left, I'd like D to be in front of C and to its left.

These functions seem to match somewhat:
http://www.ofpec.com/publicfiles/files/functions/getrelpos.sqf
http://www.ofpec.com/publicfiles/files/functions/relposcoord.sqf

Anyone have any idea how to do this?

Doolittle

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re:Relative Position account for Direction
« Reply #1 on: 04 Oct 2005, 21:38:35 »
yes. you need atan2.

Code: [Select]
_x1 = getpos obj_1 select 0
_y1 = getpos obj_1 select 1
_dir1 = getdir obj_1

_x2 = getpos obj_2 select 0
_y2 = getpos obj_2 select 1

_xdiff = _x1 - _x2
_ydiff = _y1 - _y2

_reldir = (_x2 - _x1) atan2 (_y2 - _y1)

obj_3 setpos [xcoord, ycoord]
obj_4 setpos [xcoord - _xdiff, ycoord - _ydiff]
obj_4 setdir _reldir

where xcoord and ycoord are your random coordinates (in the street :P).

basically this finds out the position of A relative to B, including direction. you may need to play with the atan2 line to get the order correct - i can't remember which one you subtract, _x1 from _x2 or vice versa.

tip of the hat to THobson for that one ;)


EDIT - re-reading your post, the above code won't take random direction of C into account... you may need to add _reldir to _dir1...
« Last Edit: 04 Oct 2005, 21:43:14 by bedges »

Offline Doolittle

  • Contributing Member
  • **
  • _this select 0
Re:Relative Position account for Direction
« Reply #2 on: 05 Oct 2005, 00:15:46 »
Thanks bedges.  I think I got it now:

Called as [You, Computer, StreetObject, D] will set D to StreetObject as Computer is to You
Code: [Select]
_obj1 = _this select 0
_obj2 = _this select 1
_obj3 = _this select 2
_obj4 = _this select 3

_dir1 = getdir _obj1
_dir2 = getdir _obj2
_dir3 = getdir _obj3
_pos1 = getpos _obj1
_pos2 = getpos _obj2
_pos3 = getpos _obj3

_diffx = (_pos2 select 0) - (_pos1 select 0)
_diffy = (_pos2 select 1) - (_pos1 select 1)
_relx = (_diffx * cos _dir1) - (_diffy * sin _dir1)
_rely = (_diffx * sin _dir1) + (_diffy * cos _dir1)
_newx = (_pos3 select 0) + (_relx * cos _dir3) + (_rely * sin _dir3)
_newy = (_pos3 select 1) - (_relx * sin _dir3) + (_rely * cos _dir3)

_obj4 setpos [_newx, _newy, _pos2 select 2]
_obj4 setdir (_dir3 - _dir1 + _dir2)

Doolittle

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re:Relative Position account for Direction
« Reply #3 on: 05 Oct 2005, 03:48:48 »
I wrote a couple of functions that might also be of help.  SetObjPosObj.sqf finds the relative position from the reference object based on coordinate off set (how far in front or behind, how far to the left or right and how far above or below) and actually places the placement object for you as well as returns the position.  AngDirDisOff.sqf returns the relative position according to direction offset (horizontal plane), angel offset (vertical plane), and distance offset.  Find them in the attached folder.  The advantage of these functions is that they use sea level reference which yeilds more accurate relative placement for air-borne objects. For ground based objects, just omit the z coordinate part of the returned position if you want the placement object to always be placed at ground level.

Offline Doolittle

  • Contributing Member
  • **
  • _this select 0
Re:Relative Position account for Direction
« Reply #4 on: 11 Oct 2005, 07:28:55 »
Heh, now I have another problem.  If I place something on a slope....well let's just say if you try to put an M2 in a perfect spot inside a bunker, it depends on the slope of the ground too!!  This is hard to explain without hand signals.

I will post my solution if I get it...

Doolittle

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re:Relative Position account for Direction
« Reply #5 on: 19 Oct 2005, 01:03:22 »
Vehicles will automatically tilt when they are place on a slope from the editor.  When you use setPos from a scipt they sometimes do not tilt with the slope of terrain, but I found a way to work arround this.  On the line right after they are setPos, give them a small downward velocity (obj setVelocity [0,0,-1].  This will make the vehicle bounce a bit, but then it settles on the slope.

Offline Doolittle

  • Contributing Member
  • **
  • _this select 0
Re:Relative Position account for Direction
« Reply #6 on: 29 Oct 2005, 18:28:37 »
Actually I'm placing an unmovable object, like a gun...

I found out how to do it... by checking the sea level height of the objects and adjusting the difference to account for slope.  :)

It works nice... Now I can place a bunker and have a gun right at edge of bunker & the gun isn't sunk way down if the bunker is on a massive slope.

Doolittle