Home   Help Search Login Register  

Author Topic: myobject distance [_x,_y,_z]. How?  (Read 1084 times)

0 Members and 1 Guest are viewing this topic.

Offline Carl Gustaffa

  • Members
  • *
  • I'm a llama!
myobject distance [_x,_y,_z]. How?
« on: 23 Dec 2005, 21:07:36 »
Hi

I realize that the distance operator is used to find the distance between two objects. But I now have a calculated position using given position and randomness, and I want to check the resulting distance.

How would I do this?

I tried "Abrams" createVehicle [_x, _y, _z] (something like that), in an earlier experiment, but that would apparently snap the tank to the ground (ignoring the z coordinate), so I guess creating and deleting a temporary object for distance measuring would fail. Funny thing is though, that bombs and flares and such are created where they should.

Did I miss something in scripting class? :)

Algebraic approach?

All help appreciated.

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:myobject distance [_x,_y,_z]. How?
« Reply #1 on: 23 Dec 2005, 21:41:59 »
Easiest thing you can do is to place a gamelogic at the position you want to measure distance from. It stays at the position. Then you can use distance in the usual way because now you have two objects. It is best to first create a gamelogic (if you don't already have one in your mission, which would be ideal to use) and then use setPos to set the gamelogic into the desired position. This will give you more reliable results than creating the gamelogic straight to the desired position.

Actually the height value will vary a little if you query the gamelogic's position multiple times, but it is not relevant because the variation is very small. Just don't make assumptions anywhere in your code that the height value would always be exactly what you first set it to.
« Last Edit: 23 Dec 2005, 21:44:49 by Baddo »

Offline Carl Gustaffa

  • Members
  • *
  • I'm a llama!
Re:myobject distance [_x,_y,_z]. How?
« Reply #2 on: 23 Dec 2005, 21:42:37 »
I'll answer myself this time :D Got the (needed) math help on IRC, then it was no problem:

Code: [Select]
; _dx, _dy, and _dz obtained from other object + randomness.
_dx = (getPos myobject select 0) - _px
_dx = _dx * _dx
_dy = (getPos myobject select 1) - _py
_dy = _dy * _dy
_dz = (getPos myobject select 2) - _pz
_dz = _dz * _dz
_distance = sqrt (_dx + _dy + _dz)

Offline Carl Gustaffa

  • Members
  • *
  • I'm a llama!
Re:myobject distance [_x,_y,_z]. How?
« Reply #3 on: 23 Dec 2005, 21:46:17 »
Oh, I should mention that it was used within a fire event handler. Just to make artillery take much longer time to shoot down aircraft, using explosives close (but not too close, that was the issue) to the aircraft, and removing the actual bullet that the unit fired.

Looks pretty cool :)

I've seen some threads about game logic, but I'm not sure what the deal about it is. But I'm probably getting there, this is sort of my first script/mission I'm messing with :)

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:myobject distance [_x,_y,_z]. How?
« Reply #4 on: 23 Dec 2005, 21:52:03 »
Oh, OK.

:)

Gamelogic is a very good invisible helper when you need to get distance between positions. Also it is good for many other purposes like filling vehicle positions with gamelogics so you can restrict where AI / players are able to sit in a vehicle.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:myobject distance [_x,_y,_z]. How?
« Reply #5 on: 23 Dec 2005, 21:56:23 »
That formula will work if height of the land above sea level is the same under each location.  You see the problem here is that:
getPos myobject select 2
will return the height of myobject above ground level so if one of your objects is above the mountains and one above a valley you will get an incorrect result.

There are two ways round this (cue people telling me about a third) ;D

1.  By far the simplest is the one Baddo describes.  Put a GameLogic at each point and use the distance command

2. Move a trigger under the location of each point:
trigername1 setPos [getPos myobject1 select 0,getPos myobject1 select 1,0]
ditto for myobject2
Then you can use your formula except that for the Z coordinate you would need to use:
(getPos myobject1 select 2) - (getPos triggername1 select 2)

The reasoning behind this is that if you move a trigger by setPos [x,y,z] then the trigger is placed z above sea level not ground level.  So if you use one trigger for each object you will be measuring z above seal level for both.
« Last Edit: 23 Dec 2005, 21:58:35 by THobson »