Sign in to follow this  
uralkemal

a problem with projectiles

Recommended Posts

Hi i am writing a game using projectiles like worms:). But there is a problem with it. The maximum range must be at 45 degrees for a launched object. But i have problem with it. For example launched object goes in 10 degrees further than 45 degrees. codes: xpos=xpos+cos(angle)*speedx ypos=ypos+sin(angle)*speedy gravity=gravity+0.1 #optional speedy=speedy-gravity #basics of the code thanks...

Share this post


Link to post
Share on other sites
Your formula is off. The cosine and sine of the launch angle should only be used to initialize the x and y velocities. They should not appear after the initialization at all.

Share this post


Link to post
Share on other sites
i think i understand what you mean. But can you explain a bit?

edit: Oh it was really helpful! I did it, thanks for your help...

Share this post


Link to post
Share on other sites
There are other problems: "gravity=gravity+0.1" is wrong. Acceleration due to gravity should be constant. Also, your formulas are not correct (even after computing the initial velocity). Here are the correct formulas for constant acceleration:
    speedy = initial_speedy - gravity * time
ypos = initial_ypos + initial_speedy * time - 0.5 * gravity * time * time
time can be measured from when the projectile was launched or from when the previous frame was displayed, as long as initial_ypos and initial_speedy are computed when time == 0.

Share this post


Link to post
Share on other sites
Hey, its a must to work with vectors in your situation. If you dont know much about vector math google for it. Better do this way:


//the vector structure
struct VECTOR2
{
float x, y;

VECTOR2(float xx, float yy)
{
x = xx; y = yy;
}

//do some operators for it
VECTOR2 operator + (const VECTOR2& v)
{
return VECTOR2(x + v.x, y + v.y);
}

VECTOR2 operator += (const VECTOR2& v)
{
x += v.x;
y += v.y;
return (*this);
}

.
.
.
}

.
.
.

//then, integrate the position|velocity(they're VECTOR2s) using Euler method(enough for constant acceleration
position += velocity*dt + 0.5f*gravity*dt*dt;//velocity is still v0 here and gravity is the vector [0, -9.81]
velocity += gravity * dt;


Hope this helps

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