aiming? could call it homing missiled I guess

how do I make the bullets go where I click. but at an angle. not jsut going along the x axis then the y.

thx in advance.

EDIT: this is not the only thing I would use it for, but I think that the same thing would work for all of my ideas.

  1. calculate the direction
  2. normalize it
  3. multiply it by the speed
  4. add it to to the position

how do I calculate the direction is what I am asking(sry if not direct enough).

thx for fast response.

You could use Math.atan2(y, x) to get an angle, then rotate the speed around that angle.
Or calculate the slope (y2-y1)/(x2-x1) and normalze that - beware of division by zero.

is the x and y int he first formula the x distance and y distance?

this is 2d vector math. look it up.

  1. calculate the direction:
    substract the origin from the destination point

  2. normalize it
    divide the result vectors components by the vectors length

  3. multiply it by the speed
    multiply the components by the desired amounts of pixels to move per frame

  4. add it to to the position
    add the result vector to the origin point

It explains what a vector is

thi sis the most explanation on moving down the line I get though

[quote]Note that we’ve simply moved the point at <5,5> in the direction <4,-4>.
[/quote]
It never says how to do it though.

Here is a good interactive on 2D vector math.
http://demonstrations.wolfram.com/2DVectorAddition/

I feel. dumb. This is slopes right? cause that is waht it apears like to me.

I jsut learned this in math like 2 weeks ago. I am dumb…

how can I simplify the fraction I get.

like how doI simplify vy/vx?

cause the ships jsut fly through the screen so i cannot see what theya re doing.

Vector math deals with slopes somewhat.
If you are adding two vectors, you start at 0,0 and draw a line with the slope of the direction of the vector and a length of the magnitude of the vector (think direction and speed) That will end at a point x,y. Draw the second vector in the same manner with the point x,y as its starting point. This will end at a new point x’,y’. Your new vector that is the sum of the other two is the vector from 0,0 to x’,y’

The link I posted allows you see that in real time.

A solution WITHOUT Vectors, I used in a 2D game of mine:

int diffX; //the distance in X-direction from the bullet to the enemy
int diffY; //the distance in Y-direction
double maxDistance; //the maximum air-line way the bullet is allowed to travel in this frame (e.g. timedelta * 100 pixel/millisec)
double distance = Math.sqrt(distX*distX + distY*distY); //the exact, air-line distance (calculated with Pythagoras' theorem)

double goX = diffX*distance/maxDistance; //the x-way which the bullet travels in this fram
double goY = diffY*distance/maxDistance; //the y-way

this.x += goX;
this.y += goY;

Good work!

But


double goX = diffX*distance/maxDistance; //the x-way which the bullet travels in this fram
double goY = diffY*distance/maxDistance; //the y-way

has to be


double goX = diffX/distance*maxDistance; //the x-way which the bullet travels in this frame
double goY = diffY/distance*maxDistance; //the y-way

And btw. this is what vector math would do under the hood :slight_smile:

h3ckboy, I don’t know how old you are, but it would be a good idea to start looking into the basics of trigonometry and kinematics. Vectors and motion are vital to game development, so understanding these things will prove immensely useful; besides, it cant hurt to get ahead ;D

just a notice, not sure but you may have some trouble this way, double/float dont like to be integrated

you should better use something like :

...
int step
...
...
...
this.x = this.x0+ goX*this.step;
this.y = this.y0+ goY*this.step;
this.step++;

A Vector is just a direction of a certain length (have I already explained this recently?). A unit vector is what you get when you normalize any vector - it has a total length of 1, and therefore can be considered of only having a direction. So just like if you’re only going along 1 dimension, you could either being going right (1) or left (-1). Then if you multiply that my your speed, you’ll get something moving right or left as far as you wanted it to go.

A slope is not even directional, it is angle-related. This is because a slope determines a line, and a vector is more like a ray or line segment except it can exist anywhere in space. A vector simply denotes a single movement, but it doesn’t denote where. A vector with a value of (1,1) would specifically move towards the up-right. A vector with a value of (-1,-1) would specifically move towards the down-left. A slope of (1,1) and a slope of (-1,-1) are equivalent, however, because a slope is really a single number which is y/x, and 1/1 == -1/-1 == 1.

So, let’s review.

Vectors

  • Denote a single movement of a given direction and a given distance.
  • Are completely position independent.
  • If they have a length of 1, they are a unit vector and therefore only denote direction.
  • Can be in any number of dimensions, from 1D to infinityD, although typically will be in 2D or 3D, or 4D when they’re usually used as Quaternions.
  • Are necessary for anything movement related in a game.

Slopes

  • Denote an angle for a line within coordinate space.
  • Are equivalent to the tangent of the angle from the X axis.
  • Have no length or distance. The greater the value, the steeper, the lesser the value, the flatter.
  • Can only be in 2D (although I guess you could figure out some equivalent for 3D).
  • Are pretty much useless to game development.

Wait cant you use a slope to figure out the y/x of the slope, and then go y down and x left(in am including negativews here)

and -1/-1 is different than 1/1.

y+=-1

is dif than

y+=1

P.S: I am in pre-al lol

You’re not actually using the slope.

Slope = “rise over run” = y / x. The value of y / x. It does not have both an X and a Y component. 1 / 1 = 1. -1 / -1 = 1. Therefore the two are the same if you’re talking about slope.

A vector has direction as mentioned above. For a vector, 1,1 is different from -1,-1.

Just never refer to the slope again, because you’re never going to use it. You’re going to use vectors.

wait so is what I am doing a vector than?

cause wha tI did is just have a theoretical “fraction” and then jsut have it as x and y.

I have gone farther with this, and I do this:

vx = vx/vy;
vy = vy/vy;

I am having a problem wiht this. What happens is taht if they are both negative they become positive :/.

I ahve fixed that problem wiht an if, but when one if positive and one is negative it makes them both positive :(.

thx for you help :slight_smile:

My serious advice is that to get the best help, you should read all the replies in your threads regarding this problem, very carefully.

It might sound lame, but we’ve answered all your questions. You just haven’t realized it. There is not much else to explain for us (unless anybody else has an idea).

You might want to read a few articles on what vectors are, and start from scratch with your code. Any code that handles your ‘slopes’ will only waste your time. There is nothing to improve there, just replace it with vector code - once you have a sound understanding of them - they are not that hard to grasp.

I hope you don’t feel insulted (or otherwise misthreated) by this post - it is certainly not my intension.

Keep up the spirit, burn your ships (and read a few articles :))