Odd velocity numbers

Started by
10 comments, last by fooman_69 18 years, 10 months ago
Hello, I am doing simple parabolic projectile work, but I am still new with all this. Anyways, I start off with 270 degrees and a velocity of 50(pixels per second on screen). I use these equations that I got from reading an online math course to get the intitial velocities for x and y:
vx = GetVelocity() * cos((float)GetAngle());
vy = GetVelocity() * sin((float)GetAngle());
But vx ends up as 49.2191, and vy comes out as -8.8023. Does anyone know why I would get another answer other than 0 for vx if 270 is my angle... since cos(270) = 0 correct? Anyways, any help is always welcomed!
Advertisement
Quote:Original post by fooman_69
Does anyone know why I would get another answer other than 0 for vx if 270 is my angle... since cos(270) = 0 correct?

The cosine for 270 degrees is 0, yes. But cos() takes radians, and cos(270) = 0.9844. You must convert the angle to radians before passing it to the functions. You do that bt multiplying your angle in degrees by pi/180.
Are you working with radians or degrees?
Thanks!!!
I am workin with degrees.... thank you SO much!
I figured I was using cos() incorrectly.
Is this still incorrect?? I am still evaluating to negative answers.
void Weapon::DetermineVelocities(){	float radian = (float)GetAngle() * ((float)PI / 180.0f);	vx = GetVelocity() * cos(radian);	vy = GetVelocity() * sin(radian);	Show(); //debug stuff}

cos 270deg = 0
sin 270deg = -1

Maybe you need to put a negative sign before sin?
I just cannot figure this out. Here is some code from my Weapon class that basically figures out the initial velocities based on the angle of the gun and the power of the gun.
Then every frame UpdateXY is called with the time. startTime is already set earlier. I am getting some really stupid numbers all around, x AND y are really incorrect.

void Weapon::DetermineVelocities(){	float radian = (float)GetAngle() * ((float)PI / 180.0f);	vx = GetVelocity() * cos(radian);	vy = GetVelocity() * sin(radian);}void Weapon::UpdateXY(float time){	float diff = time - startTime;	float newX = GetMapX() + (vx * diff);	SetMapX(newX);	float newY = GetMapY() + ((vy * diff) + (0.5f * (float)(-GRAVITY) * pow(diff, 2)));	SetMapY(newY);}


I start with 270 degrees (not radians), and a velocity of 50.
The gravity calculation is supposed to be negative right? I am using pow() correctly? It is correct to use the time difference instead of just the time the function is called ... right? haha, this has me questioning a lot about this simple code!
Please, ANY help is amazing!!
Not sure where the problem is, but...

After you calculate diff = time - startTime, are you resetting 'startTime' to 'time' somewhere?

GetVelocity() should probably be named GetSpeed(). Velocity is a vector, and has magnitude and direction; that's what your vx and vy are. Speed is a scalar, i.e. a single value, which I assume is what your function GetVelocity() returns.

Is your coordinate system set up with x to the right and y up? However it's set up, the following angles will point along the following axes:

0 degrees : +x
90 degrees : +y
180 degrees : -x
270 degrees : -y

If that isn't what you want you'll have to adjust the components of your velocity vector appropriately.
How do I use vectors appropriately?? I have seen random examples, but I am lost on how to use the concept for what I need to do.
Clippy: "It looks like you're trying to make a projectile simulator."


One easy method that I like is this:

Step 1: Determine the launch velocity vector (identical to what you have so far with the vx, vy cos and sin stuff)

Step 2: Each update, modify your existing velocity (vx, vy) by any acceleration (gravity)
vx += acceleration_x * time_step;vy += acceleration_y * time_step;// time_step is the amount of time that has passed since your last update, in time units// rather than the amount of time since you launched the projectile.// (seconds?  milliseconds?  whatever you choose.)


In your case, acceleration_x would be 0, and acceleration_y would be some value that represents gravity (positive or negative, depending on which way it "down" in your sim).

Step 3: Just after you modify velocity, update your position in pretty much the same way.
x += vx * time_step;y += vy * time_step;


Step 4: Draw your projectile and repeat at step 2.


This ends up doing almost the same thing as the y = y0 + (vy0 * time) + (0.5 * gravity * time * time) equation that you look like you're trying to use.



Notice how both the X and Y lines are basically identical other than X and Y? If you make a class (or structure) that contains both X and Y and overloads the addition and multiply operators, you can simplify the code:
velocity += acceleration * time_step;position += velocity * time_step;// if velocity, position, and acceleration are Vector objects with overloaded math operators, then this will do the same thing as above.


That's the main (and only?) benefit of vectors. They make your code nicer to read and quicker to write.

This topic is closed to new replies.

Advertisement