Sign in to follow this  

Adding to a vectors magnitude--Help

Recommended Posts

Hey, i have a 2D game in which bullets have a X-velocity and a Y-Velocity. I was wondering how i could make them accelerate by adding to the magnitude of each vector each timestep. The only problem is, how do I add to the magnitude? if it proves to be to cpu intensive, i could just multiply it, but then you have linear acceleration and i want constant. Thanks

Share this post

Link to post
Share on other sites
Unless you have 100k bullets, i would not care about performance...

To increase magnitude you can

or (that's the same but bit faster and written per-component)

double s=sqrt(v.x*v.x+v.y*v.y);
v=v*s;//do it per-component.

It will accelerate 'em kinda like rockets.

If you want to simulate air resistance, it's proportional to speed squared:
where k it's viscosity.

Entire thing can be written shorter as

BTW. Some clarification/rant really needed there:

Instead of velocity, keep a directional vector (of length 1) and the bullet's speed. The velocity is just direction * speed.

And gravitation is just what?

Storing velocity as direction and speed is generally not a good idea. In most cases it's significantly slower. The only case you may get some benefit is particle systems with air resistance, and in zero gravitation. Also it's really ALOT worse than velocity vector if you'll try to someday swich from Euler integration to something better (RK4).

Simple example where it's slower:
Say, you store your speed as
struct velocity{vec d, double s;}
where d is unit-length direction vector, and s it's speed.
And you need to simulate gravitation.
Let z points to up.
So you have to
vec v=object.velocity.d*object.velocity.s;
object.velocity.s=Length(v);// square root
object.velocity.d=v*(1/object.velocity.s);// inverse

See? For extremely small benefit with air resistance and similar things, it's slower and harder to do simplest and most common things, such as gravity, or accelerations due to external forces. Not to mention memory and multiplications.
So i doubt that anyone proposing direction/speed scheme really tried it in practice, because, it looks like it gives benefit only until you think bit more about it, or at least try to implement.

Even worse if you'll try to store in that way everything, including acceleration.

As about accelerating, say, rocket. Rocket have it's frame of reference. If nozzle points to -z , to simulate acceleration you need to

Share this post

Link to post
Share on other sites
yeah, you may think that sounds crazy, but once the bullets leave the screen they are deleted so this method is acceptable to me. And besides, most space games do not have gravity.

I have seen a really cool game for gravity modeling in space, i cant rememebr what it was called, but it was sort of like worms except you were a spaceship in this 2d solar system full of huge planets, and you chose from an array of weapons and fired them at your enemy, the bullets curved around planets.

Share this post

Link to post
Share on other sites
It's simple to do, in fact.
If you have bullet at point p, to find acceleration of bullet you need to do something like

vec2d accel=vec2d(0,0);
vec2d dist=massive_bodies[i].position-p;
double r2=dot_product(dist,dist);
double r=sqrt(r2);
// add there checks if r< size of planet.(if so, billet can be removed)
//accel=accel+massive_bodies[i].gravity*(dist*(1/r))/r2; - unoptimized
accel=accel+massive_bodies[i].gravity*dist*( 1/(r*r2) )

and then bullet.velocity+=accel*dt;
Where dt it's time of current frame - time of prev. frame. (or some fany thing to compensate timer issues)

Same applicable to planets, spaceships, etc, everything including bullets.

Computers are fast enough to simulate throusands bullets without any lag... so you can make it fire MANY bullets after getting some bonus capsule(that also must fly on orbit), for fun.
Also bullets will orbit around planets and may hurt ship that fired 'em...

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this