Jump to content
  • Advertisement
Sign in to follow this  
fooman_69

Odd velocity numbers

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Are you working with radians or degrees?

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!