Directional Problem

Okay, for those of you who have been following my questions, its obvious I have trouble comprehending movement for some odd reason. So here is yet another question:

How do I make an object change its direction and velocity to move towards a specific location? For instance, I have a bot that moves around detecting targets, no problem. I want it to fire a projectile AT the target when found. If I have the target's location etc., how do I make something head for it?

Thanks,
Moto

Directional Problem

To get the vector representing the direction from one agent to another, you'll just subtract the first agent's location from the target agent's location.
Typically, you'll then want to normalize the result to make it be of length 1.0 and rescale it by your desired velocity.

toTarget = (target get-location) - (projectile get-location).
toTarget /= | toTarget |.
newVelocity = desiredSpeed * toTarget.

projectile set-velocity to newVelocity.

You might want to look at the Swarm demos, as they employ this type of logic (in a more complicated way, because there are many simultaneous velocity vectors at work).

- jon

Directional Problem

That works great, thanks! I'll take a closer look at the Swarm demos to learn more.

~Moto

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.