Adding to a vectors magnitude--Help

Started by
5 comments, last by Dmytry 19 years, 6 months ago
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
Advertisement
Instead of velocity, keep a directional vector (of length 1) and the bullet's speed. The velocity is just direction * speed.

Unless you have 100k bullets, i would not care about performance...

To increase magnitude you can
v=v+acceleration*dt*Normalized(v);

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

double s=sqrt(v.x*v.x+v.y*v.y);
s=(s+acceleration*dt)/s;
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:
acceleration=-k*(v.x*v.x+v.y*v.y);
where k it's viscosity.

Entire thing can be written shorter as
v=v*(1-dt*k*sqrt(v.x*v.x+v.y*v.y));


BTW. Some clarification/rant really needed there:
Quote:
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;
v.z-=g*dt;
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
rocketVelocity+=dt*acceleration*rocketRotationMatrix.3rdcolumn;
well this is a pretty simple game so i think that hh10ks idea would be easier and less calculation intensive than yours dmytry becasu this is just a top down shooter with almost no physics
without gravitation at all, but with accelerating bullets?
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.
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);For(i=0;i<massive_bodies.size();i++){  vec2d dist=massive_bodies.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.gravity*(dist*(1/r))/r2; - unoptimized  accel=accel+massive_bodies.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...

This topic is closed to new replies.

Advertisement