Missile Pathing for 2D game

Started by
26 comments, last by FLeBlanc 11 years, 3 months ago
I did a little test using Lua Love2D. alvaro is correct in that you don't need to keep a separate speed variable if you don't normalize your (vx,vy) velocity vector. Here is some quick video of the test using a field 640x480, initial missile velocity of (1,0), spring of 3 and damping of 1 against a circling target:

[media]
">
[/media]

The missile updating code:


	local dx=target.x-missile.x
	local dy=target.y-missile.y

	local accelx=missile.spring*dx-missile.damping*missile.vx
	local accely=missile.spring*dy-missile.damping*missile.vy

	missile.vx=missile.vx+ dt*accelx
	missile.vy=missile.vy + dt*accely
	
	missile.x=missile.x+dt*missile.vx
	missile.y=missile.y+dt*missile.vy


It's pretty much just as written in his initial post, without normalization of (dx,dy) and without an external speed variable. You do have to play with the spring and damping constants quite a bit to get something that feels right.
Advertisement
It works now. Many thanks to all of you :)
And thanks for not giving up on me xD

Bump!

There is an other thing, about the missiles rotation.

Currently in my game, the missile is always rotated so it is pointing at the target. Instead, I want it to rotate like the youtube click above posted by JTippetts.

In the clip, the missile rotates towards its direction.

How to achieve this?

Make the missile point in the direction of its velocity.

Do you mean that the missile should point at the velocity vector?

I mean exactly what I said. You point at a point or in a direction; velocity is a vector, so you point in that direction.

Oddly, that didnt work.


rotation = (float) getAngle(centerX, centerY, vx, vy);

public static double getAngle(float x1, float y1, float x2, float y2)
{
     float deltaX = x2 - x1;
     float deltaY = y2 - y1;

     return Math.atan2(deltaY, deltaX) * 180 / Math.PI;
}

The missile's direction wouldn't be from (centerx,centery)->(vx,vy). (vx,vy) indicate the direction the missile is going. That is the direction, and that is the vector you need.

angle=atan2(vy,vx)

This topic is closed to new replies.

Advertisement