Archived

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

2D Trajectory Problem

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

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 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).

Share on other sites
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 on other sites
Hey thanks. I added this line of code:

angle = (PI * angle) / 180;

No angle problems now!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633705
• Total Posts
3013464
×