# 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 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 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 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 on other sites
I didn t know how to use accelerations. Thanks John... :)

##### 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 structurestruct 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 accelerationposition += 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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627689
• Total Posts
2978648

• 13
• 14
• 12
• 10
• 12