aiming? could call it homing missiled I guess

I think Riven is right, but let me try to be more clear one last time.

A slope in the context of a game will essentially give you an angle. It does not have both X and Y, it is a single value. In order to handle movement on a 2D plane (which is what you have), you need something that has both an X and a Y. In other words, a vector.

Although we’ve mentioned all these things you can do with vectors, the actual vector itself is very very very simple.


public class Vector2D
{
     public float x;
     public float y;

     public Vector2D(float setX, float setY)
     {
          x = setX;
          y = setY;
     }
}

Seriously, that’s it. Anything else in a vector class has nothing to do with the actual structure of a vector, instead it has to do with different things you can do with it. Just having these two floats, you can represent absolutely any movement in 2D space, be it left, right, up, down, or in between. And it can cross half a screen or 5000 screens. It’s very simple but can do absolutely anything you want in terms of movement.

So. You create your vector initially with the positions of the mouse and the cannon.


//This will give you a vector that points FROM the cannon TO the mouse. If you subtracted them in reverse order,
//the vector would point in the opposite direction.
Vector2D velocity = new Vector2D(mouse.getX() - cannon.getX(), mouse.getY() - cannon.getY());

//Use the pythagorean theorem to get the length of the vector.
float length = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);

//Normalize the vector (making it have a total length of 1, turning it into a unit vector) using the length we found.
velocity.x /= length;
velocity.y /= length;

//Multiply the unit vector by the power of the cannon, so the more power there is, the further it flies.
velocity.x *= cannonPower;
velocity.y *= cannonPower;

//Finally, create a bullet with the initial velocity of what we just computed.
world.add(new Bullet(cannon.x, cannon.y, velocity));

That’s as much as anyone can baby you. I’ll answer any questions you specifically have about the code mentioned above, but I won’t explain vectors any further (and I doubt anyone else will, either).

Thank You!

that has to be the most helpful thing any1 has ever told me!

I finally get it! :).

I didnt realize it was so simple.

I was almost doing that already. I just didnt have a seperate class for it.

I am gunna try tweaking my game with the info you jsut gave me.

will post back with any questions.

THANK YOU!

youa re a genious!

I feel dumb now :/.

hehe I will try to figure stuff out more, so that you guys dont ahve to help me as much.

thx.

Glad that helped, no problem. I figured maybe you just needed to look at some simple source code.

Actually, i just ran into this. Until now i’ve been keeping track of stuff via things like double x, double y, etc. Would i take a noticeable hit by using Point2d and Vector2ds instead? It seems like a much nicer way to keep track of things but there’s going to be lots of projectiles on screen and i don’t want to unnecessarily slow things down (this is to be a VERY VERY fast paced game). Also the solution to his question answered mine before i asked, but also if i wanted actual HOMING shots like a self guided missile ( as opposed to a automatically targeted turret shot like what the parent post seems to be talking about ) that would follow an evasive target what should i do? ( with limited turn angles that i can set [how would i do that?] )

“You could use Math.atan2(y, x) to get an angle, then rotate the speed around that angle.” Is it related to this?

Java’s very very good at cheap object management, so using Vector2 should not slow things down at all, especially in any game that one human being can possibly make on modern hardware.

As for your question, go back and read more closely. You never want to use the slope. Never, ever, never. Just use a Vector2.

Here’s what you do for a homing missile, in pseudo-code.


Vector2 velAdjustment = missilePos - missileTargetPos;
velAdjustment.normalize;
velAdjustment *= missileSpeed;
missileVelocity += velAdjustment;

A homing missile is exactly the same as a ballistic missile except that it can constantly adjust its velocity. A ballistic missile only gets fired once using a given velocity, then stays on that course forever.

Also, for anyone else reading this thread, ignore this post (with all respect to Hansdampf, because I think he was just answering a non-pointed question). I don’t think it’s at all a good direction to be thinking. You don’t want to use atan at all and you never want to use the slope at all. Use vectors.

ah right… that makes senses lol thanks a bunch ( for some reason i realized i could make a laser rigidly bend towards the enemy by redoing that tracking caluclation halfway through it’s life but i didn’t notice that you just repeatedly do that for full homing )

Just curious why slopes are so bad? Is calling sin/cos/atan slow? Or is it just bad practice?

My current code is this:


x += Math.sin(direction) * speed;
y -= Math.cos(direction) * speed;

The reason I did it this way was because it is really easy to modify the direction and speed based on the user keyboard input (just increase/decrease them). Would you still recommend using vectors in this situation?

Thanks.

It is slightly slower to no real detriment, but I was just telling the noobs to stay away from it because it was giving them the wrong idea of how they should be approaching this. :stuck_out_tongue:

Oh, cool. No worries. That makes sense. Cheers. :slight_smile:

to clarify, the only differenec I can notice in vector2D’s, and my messed up slopes, is that waht you did was in a seperate class, and it was normalized. I did not know how to normalize. so I was just picking a directoin and firing away.

Ok, forget about the slope thing.
I dont think that using atan2 is evil (unless you move 1 million missiles).
Using atan doesn’t prevent you from using vectors. In the case of a homing missile you SHOULD use an angle if you want to have it easy.
Here the pseudo code (I did not test it) for a homing missile with a maximum turn speed:


class Missile{
  V2 pos;
  double angle,turnspeed,frontspeed;

  public void turnTo(V2 target,float deltaTime){
     double angleToTarget=getAngle(pos,targetPos);  //getAngle uses atan2 here
     double delta=angle-angleToTarget;
     if(delta>0)angle-=turnspeed*deltaTime;
     else angle+=turnspeed*deltaTime;
  }

 void move(float deltaTime){
    V2 speed=new V2(0,frontspeed*deltaTime); 
    speed.rotate(angle);  //thats what I meant by 'rotating the speed'
    pos.add(speed);
  }
}

This will work for an accelerating homing load of mud, don’t do that for a homing missile, that will look very unrealistic.

You might need to dampen die velAdjustment change a bit, to make look more realistic, but otherwise it’s a reasonalbe approach.

But not if you want a fixed turnspeed. Homing missiles don’t acc/decelerate, they chance the direction.
If you fire a missile in the direction (1,0) and want to hit a target in (-1,0):

demonpants missile will stay in the same 1-dimensional ‘line’ and will decelerate until it has speed(0,0), then accelerate again.

my missile makes an elliptical turn

Ah ok, you are right. I overlooked the “missileVelocity += velAdjustment” line. I think this was a type - this way the missile would accelerate to light speed anyway :slight_smile: it should of course be


Vector2 v = missilePos - missileTargetPos;
v.normalize();
v *= missileSpeed;
missilePos += v;

This equation does not have a fixed turn rate, but you could have some good effects with applying the exponential smoothing (thanks bleb ;))to the velocity change:


float a = 0.7f; // smoothing factor
Vector2 v = missilePos - missileTargetPos;
v.normalize();
v.x = a*v.x+(1-a)*lastV.x;
v.y = a*v.y+(1-a)*lastV.y;
v.z = a*v.z+(1-a)*lastV.z;
v.normalize();
v *= missileSpeed;
missilePos += v;
lastV = v;

hm… I don’t think that will solve that ‘missile moves like a magnet’ thing, but who cares how a missile moves as long as it hits the target :slight_smile:

it will, since the direction change is smoothed. Actually it will look more realistic than limited turn radious, since it’s turn rate adjustes with the target movement.

[quote]If you fire a missile in the direction (1,0) and want to hit a target in (-1,0):
demonpants missile will stay in the same 1-dimensional ‘line’ and will decelerate until it has speed(0,0), then accelerate again.
my missile makes an elliptical turn
[/quote]
I can’t see see the y (or in 3D including z) component of speed change with your method, it will always be 0.
Maybe I’m wrong, just tell me. Of course you could add some jitter/chaos to prevent that case.