2DWorlds Forums
Using velocity to move one object towards another. - Printable Version

+- 2DWorlds Forums (http://2dworlds.buildism.net/forum)
+-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4)
+--- Forum: Scripting (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=13)
+--- Thread: Using velocity to move one object towards another. (/showthread.php?tid=2188)



Using velocity to move one object towards another. - Scripter - 04-04-2011

This is mostly a math question, but how would one use FixedVelocity to move one object (In this case, a Circle) towards another (Player's body)?

I really want to finish my Chain Chomp ^_^


RE: Using velocity to move one object towards another. - Jacob__mybb_import1 - 04-05-2011

This is adapted from my gravity tool script. It hasn't been tested.

[lua]
force = 5
unitVector = box1.PositionConfusedub(box2).normalized --Get a vector with length 1 that points in the right direction
box1.FixedVelocity.Velocity = unitVector:mul(force) --Multiply it by the force to get the new velocity
[/lua]


RE: Using velocity to move one object towards another. - Scripter - 04-09-2011

Thanks Jacob_! By making the force a negative, I managed to get it to work Tongue

By the way, I'm curious if there is a way to move it only one way (up or down). I am also curious as to what :mul() is.


RE: Using velocity to move one object towards another. - Jacob__mybb_import1 - 04-09-2011

If the x is 0, it will not affect x movement; same for y,

mul() returns a copy of the vector with the x and y components multiplied by the given value.