Archived

This topic is now archived and is closed to further replies.

2D Trajectory Problem

This topic is 5148 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

Does anyone see anything incorrect about this code? The 2D trajectory is inconsistant and sparatic and the missile fires all over the place... here is the code: bool GameClass::FireMissile(Sprite *sprite) { RECT temp_rect; temp_rect = sprite->GetRect(); // Calculate the angle of the shot, out of 180 double angle = (ANGLE_MULT * (80 - TANK->GetAngle())); // Calculate the velocities in vectors C_dPoint Velocity; Velocity.x = 500 * cos(angle); Velocity.y = 500 * sin(angle); // Set the missile''s velocity missile->SetVelocity(Velocity); // Set the missile''s position relative to the tank missile->SetPosition((temp_rect.right + 2), (temp_rect.top - 2)); CurState = FIRING; } This portion calculates the vectors of the velocity. The angle "bar" is out of 80 and that is why the angle is calculated by inversing it and multiplying by 2.25 (which is the value that makes 80 into 180) The following moves the missile using the velocity and gravity: if(Movement == MOVE_TRAJ) { if(ptNewPosition.y < 525) { // Add in the gravitational constant ptNewVelocity.y = ptNewVelocity.y + GRAVITY_CONSTANT; // Update position based on veolcities ptNewPosition.x = ptNewPosition.x + (m_dVelX * FrameTime); ptNewPosition.y = ptNewPosition.y + (m_dVelY * FrameTime); //UpdateRectangles(); } } Can someone please help!!!

Share this post


Link to post
Share on other sites
Ugh, you have plently of variables which I can only guess at where they are declared etc...
But as far as I can discern:
Why have you used m_dVelX & m_dVelY? Are those just assigned the values of ptNewVelocity.x and ptNewVelocity.y (because they should be). I suggest replacing the former with the latter.
Why "if(ptNewPosition.y < 525)"? Is this meant to be the floor. Not knowing your co-ordinate system, I couldn''t say for sure, but this could certainly cause odd effects. I suggest always passing this test (i.e. no floor or whatever this is mean to test for).

Other than that describe the symptoms better yada yada ya.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Make sure you are passing radians and not degress into sin() and cos()

That would quite easily cause your missiles to go every which way...

Share this post


Link to post
Share on other sites