Home   Help Search Login Register  

Author Topic: setVelocity  (Read 4340 times)

0 Members and 2 Guests are viewing this topic.

Chomps

  • Guest
setVelocity
« on: 14 Oct 2002, 19:39:27 »
Okay, so 1.85 had given us a new toy to play with... setVelocity

From what i can tell, you can use it on vehicles, and some vehicles have some restrictions.  Tanks, for example, can not be launched into the air.  It does not seem to affect people at all.

I guess you will all see this when the new ComRef is released, but I think this is how it works...

vehicle setVelocity [x-component,y-component,z-component];

Where vehicle is whatever you want to affect and the x-axis is west-east, the y-axis is north-south, and the z-axis is up-down.

Basically, it represents velocity in vector form.  If you are not familiar with vectors, here will be a rather crappy explanation in terms of OFP... (It is intentionally not mathematically precise, but we are not doing rocket science.)


##########
Crappy Vector Explanation:
(skip if you know how to deal with vectors)

Imagine a set of three-dimensional axes (three lines perpindicular to each other all intersecting a point).  One line points east (positive x), one points north (positive y), and one points up (positive z).

Imagine your jeep in OFP is standing still where all those lines meet, facing in any direction, it does not matter.  Let us say you want to launch it straight up with a speed of 50 (Anyone know what the proper units are?).  You type jeep setVelocity [0,0,50]; and the jeep springs straight up towards the heavens.

Now let us say you want to throw it to the west at a speed of 20.  Notice that the positive direction for the x-axis is east, so the y-component must be negative.  Type jeep setVelocity [-20,0,0]; and watch your jeep look like it was hit by a truck and zoom westward.

Okay, now say you want to really catapult your jeep to the North, but you want it to arc.  You want to shoot it at a speed of 100 (powerful catapult you have there) at an angle of 30 degrees from the ground.  (If you have taken physics, you are problably groaning right now.)  It helps if you draw yourself a diagram.  Draw a right triangle with a 30 degree angle.  Set it up so the right angle is to the bottom left of the diagram.  Here, up is up and right is north.  The hypotenuse is of length 100, so use your super trigonometry skills to figure out what the other sides are... Notice that you do not alter the velocity of the jeep in the west-east direction, so that value remains zero.  So you type jeep setVelocity [0,(100*(cos 30)),(100*(sin 30))]; and buckle up, you are in for a ride.

I will let you try to figure out the rest yourself...
##########


Okay, so this is a very useful command.  A couple ideas that come to mind:
- Hydraulic catapult for launching planes.
- Boosters for cars (like in Knight Rider).
- Throwing sh*t around.  ;)
- Insanely realistic mortar scripts.

One problem is that direction values are in terms of angle, where 0 or 360 degrees is north, so you will have to convert to vector notation.  Let us say you want a "nitro" script that boosts a car in whatever direction it is driving. You want to keep it realistic so you want to conserve the jeep's original velocity in the jump, meaning if the car is sliding backwards at a speed of 20 and the boost makes you go 30 forward, you only go 10 forward.  Here would be the working command for the script (SJ stands for superjeep, your cool boosting jeep)...

SJ setVelocity[(40*(sin getDir SJ))+(velocity SJ select 0),(40*(cos getDir SJ))+(velocity SJ select 1),(velocity SJ select 2)];

Try it, it works!  Now you can make it better by requiring the vehicle to be on the ground (getPos superjeep select 2 < .3) or by creating a loop so you can ramp it and simulate acceleration.

The velocity command works similar, by the way, it just returns the array containing the vector of the vehicles current velocity.  Like if you are driving a jeep straight north on a flat road at a speed of 50, velocity jeep returns the array [0,50,0].  You can use the select command with this.

Here is a little script I cooked up for seeking something out:
Code: [Select]
_seek = _this select 0
_hide = _this select 1
_speed = _this select 2

#loop

_seekpos = getPos _seek
_hidepos = getPos _hide

_diff0 = (_hidepos select 0)-(_seekpos select 0)
_diff1 = (_hidepos select 1)-(_seekpos select 1)
_diff2 = (_hidepos select 2)-(_seekpos select 2)

_vecmag = sqrt(abs(_diff0)*abs(_diff0) + abs(_diff1)*abs(_diff1) + abs(_diff2)*abs(_diff2))

_seek setVelocity [(_diff0/_vecmag)*_speed,(_diff1/_vecmag)*_speed,(_diff2/_vecmag)*_speed]

~.01

goto "loop"

_seek is the seeking object, _hide is the target, and _speed is how fast the seeker can go.  Try it out.

Discuss setVelocity here, and if you have any questions, post them or PM me, I will try to answer them.  (Finally, vector algebra is coming in handy.  ;D )
« Last Edit: 15 Oct 2002, 02:33:39 by Chomps »

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:setVelocity
« Reply #1 on: 15 Oct 2002, 00:29:21 »
I didn't know it worked with vectors. This command is gonna be heaven for the artilary buffs. Also Sefe's guided missile script could be improved drastically by this command.
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Chomps

  • Guest
Re:setVelocity
« Reply #2 on: 15 Oct 2002, 01:27:31 »
Yes, the possibilities are endless with this command.

I may start working on a homing script... calculates the position of the target and the position of the projectile and sets the velocity of the projectile accordingly.  Heheheh, imagine going through a field at night and watching a flare chase after you.

Chomps

  • Guest
Re:setVelocity
« Reply #3 on: 15 Oct 2002, 01:47:53 »
In fact, it is very simple to do a tracking script, because position is in [x, y, z] notation, so getting the displacement needed is a matter of [x1-x2, y1-y2, z1-z2]

Offline Doolittle

  • Contributing Member
  • **
  • _this select 0
Re:setVelocity
« Reply #4 on: 15 Oct 2002, 07:10:21 »
Not that it changes anything you are doing here, but in comref they have:
Quote
Position
Format:
    [x,z] or [x,z,y]
Description:
    Position, x coordinates is east-west, z is south-north, y is height above ground, default y is 0.

Where Z is second in the array.

Doolittle

Offline Dinger

  • Contributing Member
  • **
  • where's the ultra-theoretical mega-scripting forum
Re:setVelocity
« Reply #5 on: 15 Oct 2002, 07:22:03 »
Yes, but if you read the fine print it says "z is south-north", which corresponds to our cartesian y.
SetVelocity's a lot of fun, and another reason why you guys gotta learn your trig.
:D
Dinger/Cfit

Bremmer

  • Guest
Re:setVelocity
« Reply #6 on: 15 Oct 2002, 10:45:01 »
I'm having some trouble getting this to work with static objects (vehicles and such are fine). Anyone else having problems  ???

If setvelocity can't be applied to objects it would be a real let down.

Gameer_77

  • Guest
Re:setVelocity
« Reply #7 on: 15 Oct 2002, 10:55:44 »
Prepare to get let down...

I don't know why BIS didn't let us use this on objects, would have been great to throw a football halfway around Everon.  :o

 8)PEACE

Offline WhisperOFP

  • Members
  • *
  • I'm a llama!
Re:setVelocity
« Reply #8 on: 15 Oct 2002, 18:17:47 »
Another way to use setVelocity is, instead of giving speed, to stop a unit.
Usefull for helo rapelling. Tried it last night, something like :
Code: [Select]
_unitList=_this select 0
_helico=_this select 1

doStop _helico

_nbUnit=count _unitList

_i=0
#Boucle
_u=_unitList select _i

_u setPos [(getPos _helico) select 0, (getPos _helico) select 1, ((getPos _helico) select 2) + 20]
unassignVehicle _u

_u switchMove "5TDriver"


@((getPos _u) select 2 < 5)
_u setVelocity [0, 0, 0]

@((getPos _u) select 2 < 1)
_u setVelocity [0, 0, 0]

_u playMove "StandGetOutTank"
~1

_u switchMove ""

_i = _i + 1

?(_i < _nbUnit):goto "Boucle"


_helico doFollow _helico

exit

Works fine (some strange bugs I did not identify, like the need to add 20 to the height of the unit to make it appear near the helo ??? , or sometimes the last AI units having jumped being stuck & not responding to orders), can be improve greatly by adding a max height of jump (rope length), above which the unit is released and never stopped before reaching ground. OUCH!

Just one problem, impossible to land on houses, as height is calculated above ground.

Offline WhisperOFP

  • Members
  • *
  • I'm a llama!
Re:setVelocity
« Reply #9 on: 15 Oct 2002, 18:50:27 »
Let us say you want a "nitro" script that boosts a car in whatever direction it is driving. You want to keep it realistic so you want to conserve the jeep's original velocity in the jump, meaning if the car is sliding backwards at a speed of 20 and the boost makes you go 30 forward, you only go 10 forward.  Here would be the working command for the script (SJ stands for superjeep, your cool boosting jeep)...

SJ setVelocity[(40*(sin getDir SJ))+(velocity SJ select 0),(40*(cos getDir SJ))+(velocity SJ select 1),(velocity SJ select 2)];

Hmmmmm....
Problems may arise when the road is not horizontal (which is often true on Nogova ;) )
We still need somthg to get vertical orientation of the vehicle. Which can be achieved by a little trigonometry calculation, if I remenber well....no?

Chomps

  • Guest
Re:setVelocity
« Reply #10 on: 16 Oct 2002, 00:31:51 »
Quote
[quote author=WhisperOFP
Hmmmmm....
Problems may arise when the road is not horizontal (which is often true on Nogova ;) )
We still need somthg to get vertical orientation of the vehicle. Which can be achieved by a little trigonometry calculation, if I remenber well....no?

You cannot get the pitch or roll of an object, only the yaw (getDir).  The closest you can do is check for a z-axis component of the existing velocity vector to see if the object is changing altitude (driving on a hilly road).

Chomps

  • Guest
Re:setVelocity
« Reply #11 on: 16 Oct 2002, 00:36:41 »
I should ask BIS on the official forums if they could give us a command to change the orientation of an object as well as get the current orientation like in the form of an array with [yaw_angle,pitch_angle,roll_angle].  With that you could make pretty much any thing you want to happen with perfect precision, including a tail-spin script, scripting AI landings or launches from an aircraft carrier, launching and guiding a rocket, etc.

Oh, and about not moving objects, yeah, that sucks, I really wanted to make a Donkey Kong script where you throw barrels at people.

*hopes for thread to be immortalized in the Editor's Depot  ;D
« Last Edit: 16 Oct 2002, 00:39:20 by Chomps »

Offline WhisperOFP

  • Members
  • *
  • I'm a llama!
Re:setVelocity
« Reply #12 on: 16 Oct 2002, 04:06:46 »
If we have the coordinates of a vector, can't we get polar coordinates of this vector, thus having an angle?
By choosing to "merge" the horizontal components of the vector (x and y), couldn't we have a 2D vector coordinates in the plane containing z and "merge of x,y"? Then, having coordinates in this plane, we could convert them to polar coordinates, and then have the vertical angle. Not really the pitch of the object, but the pitch of it's speed vector. Less usefull, but can be used to keep vehicles on the same direction when giving them a push.

Sorry for my bad explanation, but I am not at all used to mathematics in english ;)

Whis'

Chomps

  • Guest
Re:setVelocity
« Reply #13 on: 16 Oct 2002, 04:43:06 »
I know exactly what you mean, but as you said, it certainly is not the same thing.  It probably would work for the turbo-boost example for cars, but for planes, there is a considerable difference between which way you are pointing, and which way you are moving.

Also, with a command to define the object's rotation, you can take complete control over it and script complex movements in air for choppers, for example.

Spitfire

  • Guest
Re:setVelocity
« Reply #14 on: 16 Oct 2002, 13:27:42 »
Check out a couple of script snippets I've made with setVelocity. I made sort of a Knight Rider -script discussed above, with turbo boost and jump features ;)

Also, implemented with setVelocity, there is currently a beta version available of my VTOL/VSTOL airplane script for use with Harriers and other thrust vectoring aircraft. It's a bit clumsy at the moment since vertical thrust is being controlled via action menu, but it works.

Check them out at http://www.angelfire.com/weird2/spitfire/ofp.html
« Last Edit: 16 Oct 2002, 13:38:14 by Spitfire »

Musket

  • Guest
Re:setVelocity
« Reply #15 on: 17 Oct 2002, 17:03:12 »
What would you put to get a trigger to activate at a certain speed or altitude?

Chomps

  • Guest
Re:setVelocity
« Reply #16 on: 18 Oct 2002, 00:24:38 »
What would you put to get a trigger to activate at a certain speed or altitude?

I am not sure what exactly you mean.  If you want plain speed in whatever direction, I believe there is a separate command for that (speed, I think) and for altitude you use getPos whatever select 2.

If you want the total speed as in the magnitude of its total velocity vector using velocity you would use...

sqrt((velocity thing select 0)*(velocity thing select 0) + (velocity thing select 1)*(velocity thing select 1) + (velocity thing select 2)*(velocity thing select 2))