views:

94

answers:

4
+1  Q: 

Help with vectors

I'm trying to make a game where the bullets can fly in any direction. I would ike to make them vectors with a direction and magnitude to make them go the direction. I'm just not sure how to implement this?

Thanks

+1  A: 

You could have a bullet class which contains a position, direction vector, and a velocity. Every time step you could update the bullet's position like so:

position += direction * veclocity;

This assumes that the direction was a unit vector.

Kyle Lutz
Im not sure how this translates to X and Y coordinates?
Milo
don't forget to divide velocity by a timestep, otherwise you will have crazy fast moving objects.
Craig
But what is 'position' how can i turn this into an x,y coordinate?
Milo
@User - a vector has an X, Y, Z values, if you have a vector of <1,1,1> it means to will travel 1 unit of X,Y,Z , multiply that by a velocity of say 10m/s. the object will then travel 10 units along X,Y and Z. You should divide by a time step because you updates should come at least 30 times a second.
Craig
@User a position is another vector, if you are at <0,0> you are at origin of X=0, Y=0
Craig
+1  A: 

I would create start with something like this:

struct Vector3f {
    float x, y, z;
};

struct Bullet {
    Vector3f position;
    Vector3f velocity;
};

inline const Vector3f& Vector3f::operator+=(const Vector &other)
{
    x += other.x;
    y += other.y;
    z += other.z;
    return *this;
}

inline const Vector3f& Vector3f::operator*=(float v)
{
    x *= v;
    y *= v;
    z *= v;
    return *this;
}

You can then update your Bullet position with bullet.position += velocity (vector addition is done by adding the the components separately). Note, that the velocity vector contains both, the direction and the speed (=magnitude of the vector).

And if your Bullet should become slower every frame, you can do something like bullet.velocity *= 0.98 (where 0.98 represents the fraction). Vector multiplication with a scalar is done by multiplying each component with the scalar...

Regards, Christoph

tux21b
Thanks! DirectX all makes sense now! I'm just not sure how to make the angle, say if I want it to travel at 50 degrees
Milo
Vector3f(0, 1, 0) would mean that you travel straight up (assuming that the y axis points up) and Vector(1, 0, 0) might mean that you travel left. If you want to travel at 50° you might write Vector(cos(50), sin(50), 0).
tux21b
A: 

this any use?

http://www.cs.cmu.edu/~ajw/doc/svl.html

google is a wonderful tool

pm100
I think for something as simple as vector and matrix math it would be worth his while to actually learn how to do the math. If he is wanting to get in to games he will need to know the equations and their purpose to solve problems further down the track. Such as knowing how dot product and cross products work to solve S.A.T
Craig
+1  A: 

There are two parts that need to be calculated. First, I'd start off with the total distance. This should be straightforward:

total_distance = velocity * time

Assuming this is a 2D game, you should then use sine & cosine to break the total distance up into the X and Y components (for a given angle):

distance_y = total_distance * sin(2 * pi * angle / 360)
distance_x = total_distance * cos(2 * pi * angle / 360)

Finally, the distance x/y should be offset based on the starting position of the bullet:

pos_x = distance_x + start_pos_x
pos_y = distance_y + start_pos_y

Of course, you could wrap all of this in a nice class, expanding & polishing as needed.

Eric Pi
Solved my problem thanks
Milo
this works, but not the best solution. Also didn't include magnitude or direction. imo original poster needs to learn some vector math.
Craig
My example equations do indeed include both magnitude (velocity) and direction (angle). Plus, from his comments, he was having trouble breaking down a vector into its X/Y components, so I tried to give a simple example of how to do so.
Eric Pi
ok fair enough, but User if you want to get in to game programming i would suggest getting fairly acquainted with vectors and how to use them.
Craig