Home   Help Search Login Register  

Author Topic: Basic Triangle/Distance Script  (Read 444 times)

0 Members and 1 Guest are viewing this topic.

Offline Arctic

  • Members
  • *
  • In Utero
Basic Triangle/Distance Script
« on: 23 Nov 2004, 19:03:51 »
Howdy once again! I was working on a script this morning that would track the distance of a falling object (the x and y distance of a falling chopper) from its original starting point in the air. I got most of the bugs out of the script (and will eventually try to make a calculation for the height of the chopper) but run into wierd results during the tests. For the x and y distance i get 1E010 (with a small capital E making me think the answer is in scientific notation) and a similar answer for the Total Distance (hypotenuse of the triangle).

I am bringing the two scripts here because i think you guys can answer it for me.

BTW: The origin is a gamelogic point

Code: [Select]
;calc.sqs

#start
_ocalc1x = getpos origin select 0
_ocalc1y = getpos origin select 1
_ocalc1z = getpos origin select 2
_ycalc1x = getpos bob select 0
_ycalc1y = getpos bob select 1
_ycalc1z = getpos bob select 2
_xcalc1x = getpos bob select 0
_xcalc1y = getpos bob select 1
_xcalc1z = getpos bob select 2

this exec "hint.sqs"

#alive
?(bob == objNULL): exit
#ydistance
_x1 = ((_ycalc1x) - (_ocalc1x))
_y1 = ((_ycalc1y) - (_ocalc1y))

marky = "Gamelogic" CamCreate [(_x1),(_y1),1]

leg1 = marky distance origin
#xdistance
_x2 = ((_xcalc1x) - (_ocalc1x))
_y2 = ((_xcalc1y) - (_ocalc1y))

markx = "Gamelogic" CamCreate [(_x2),(_y2),1]

leg2 = markx distance origin
#hypdistance
_hyp1 = (((leg1)^2) + ((leg2)^2))
hyp2 = (sqrt _hyp1)

#restart

~0.15
camdestroy marky
camdestroy markx
goto "alive"

And this last one is a hint format script that displays the answers:

Code: [Select]
#hint.sqs

#Start
?(bob == objNULL): exit
#hint

~0.05
hint format ["Helicopter position:\n X: %1 \n y: %2 \n Total Distance: %3",leg1,leg2,hyp2]
goto "Start"

I will be glad to learn where and what the error is! Thanks in advance!
« Last Edit: 23 Nov 2004, 19:04:47 by Arctic »

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Basic Triangle/Distance Script
« Reply #1 on: 24 Nov 2004, 08:23:32 »
Well I am not fan of de-bugging my own scripts let alone someone else's.  But I will give you an explanation of what I think is going on here.  First to answer your question.  Yes +1E10 means 1 with ten 0s.  In other words it is 10 thousand million.  

Code: [Select]
_ycalc1x = getpos bob select 0
_ycalc1y = getpos bob select 1
_ycalc1z = getpos bob select 2
_xcalc1x = getpos bob select 0
_xcalc1y = getpos bob select 1
_xcalc1z = getpos bob select 2

So _ycalc1x = _xcalc1x etc.  Is that what you really want?

Code: [Select]
_x1 = ((_ycalc1x) - (_ocalc1x))
_y1 = ((_ycalc1y) - (_ocalc1y))

marky = "Gamelogic" CamCreate [(_x1),(_y1),1]
So you have placed a GameLogic near the bottom left hand corner of the map.  x1 in and y1 up and at a height of 1.  x1 is the x distance between bob and the origin.  y1 is the y distance between bob and the origin

Code: [Select]
leg1 = marky distance originNow you calculate the 3 dimensional distance between the GL and the origin


Code: [Select]
_x2 = ((_xcalc1x) - (_ocalc1x))
_y2 = ((_xcalc1y) - (_ocalc1y))

markx = "Gamelogic" CamCreate [(_x2),(_y2),1]

leg2 = markx distance origin
Then you do it all again, and if I am correct you have placed both GameLogics in the exactly the same place.


Code: [Select]
leg1 = marky distance origin

leg2 = markx distance origin
#hypdistance
_hyp1 = (((leg1)^2) + ((leg2)^2))
hyp2 = (sqrt _hyp1)
so leg1 is the 3 dimensional distance between one GL and the origin and leg2 is the same (different GL but placed in the same point).

The description of what you are trying to do seems very simple.  I feel sure there is a simpler way of approaching it than something like this - unless I am missing something.

All of the above is while simply looking at the code I have not tested anything.

Happy to help further if you can be a bit more explicit on what you want to achieve.


« Last Edit: 24 Nov 2004, 08:24:41 by THobson »

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Re:Basic Triangle/Distance Script
« Reply #2 on: 24 Nov 2004, 09:48:06 »
Yikes!

So let me get this straight (as often I fail to manage that ;))...

You want to calculate the distance a helicopter has fallen from it's starting point?

I would use something like this:

Quote
_oldpos = (getpos heli)

@ condition <-- [size=0.25]put a condition in here to wait until the time you want to check how far the chopper has fallen[/size]

#loop
~0.5
_xmove = (_oldpos select 0) - (getpos heli select 0)
_ymove = (_oldpos select 1) - (getpos heli select 1)

; A bit of Thypagoras to get the vector distance...

_dis = ((_xmove^2) + (_ymove^2))^(1/2)

hint format ["Helicopter position:\n X: %1 \n y: %2 \n Total Distance: %3",_xmove,_ymove,_dist]

goto "loop"

Run that in a loop, and you should have your distances.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Basic Triangle/Distance Script
« Reply #3 on: 24 Nov 2004, 11:02:00 »
I too am a bit puzzled by what is required.

In Sui's script could you not just put a GameLogic at the old position of the heli and then just have

_distance = GameLogicName distance heli

or do GameLogics fall to the ground if they are placed at a height??
« Last Edit: 24 Nov 2004, 11:11:28 by THobson »

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Re:Basic Triangle/Distance Script
« Reply #4 on: 25 Nov 2004, 03:30:00 »
I think using gamelogics in his original script is the fatal flaw, as the distance command will get you the slant range to the two objects.

Gamelogics are either created at ground level or sea level (I think ??? can't remember which), so you'll end up with bad results from a distance call.

Far easier to use a bit of basic trig.

Offline Arctic

  • Members
  • *
  • In Utero
Re:Basic Triangle/Distance Script
« Reply #5 on: 27 Nov 2004, 15:27:57 »
Thanks! Ya I was trying to use trig but got confused in the process.  :-\  

After using the script, i discovered that the A10 can drift for over 9000units without fuel(!). A helo (tested on an Apache) falls about 3000units away from its orignal position after its tanks dry up.

Much thanks to both of you for helping me with the script. (I'll get good at scripting sooner or later!  ::))