简体   繁体   English

小行星游戏c ++惯性

[英]Asteroids game c++ inertia

I'm trying to make my asteroid keep moving once I press a button. 我按下一个按钮,试图让我的小行星继续前进。

void Ship::applyThrust()

{

   _v.setX(_v.getX() - sin(2 * M_PI * (_angle / 360)) * 2.5); 

   _v.setY(_v.getY() + cos(2 * M_PI * (_angle / 360)) * 2.5); 

}

This is the formula I have to help my ship move. 这是我必须帮助我的船移动的公式。

The _v.setX and _vsetY updates the X and Y position _v.setX和_vsetY更新X和Y位置

The M_PI is just 3.14. M_PI仅为3.14。

The _angle is how much of a rotation I set with my left and right arrow keys and _angle是我用左右箭头键设置的旋转量

The 2.5 is how many frames per I want it to move 2.5是我想要它移动多少帧

My ship is moving just fine, the problem is that I'm trying to simulate space inertia and have my ship continue moving. 我的船移动得很好,问题是我正在试图模拟太空惯性并让我的船继续移动。

Any ideas on how would be the logic for this? 关于如何成为逻辑的任何想法?

Thanks, 谢谢,

Inside of your game loop, you'll need a function that updates the position of the ship based on its x and y velocity. 在游戏循环内部,您需要一个根据x和y速度更新船舶位置的功能。 You're close in getting the new x and y coordinates of your ship, but you don't account for the velocity of the ship. 您已经接近获得船舶的新x和y坐标,但是您没有考虑船舶的速度。 When you apply thrust, get the x and y components of your velocity , not your new position. 当您施加推力时,获取速度的x和y分量,而不是新位置。 You'll have an additional function to update position which should be called within the game loop at a timed interval - eg every time you update the frame. 您将有一个额外的功能来更新位置,该位置应该在游戏循环中以定时间隔调用 - 例如,每次更新帧时。 So your applyThrust function should actually update your ship's velocity. 因此,您的applyThrust函数应该实际更新您的船速 That means you'll need to add variables to your Ship class for your ship's position and your ship's velocity if you don't already have them. 这意味着如果您还没有变量,则需要为Ship的位置添加变量以及船舶的速度。 I'm breaking out the components of position and velocity for simplicity, but you'll probably want to store them in a vector for clarity: 为简单起见,我打破了位置和速度的组件,但为了清晰起见,您可能希望将它们存储在矢量中:

class Ship 
{
    private float xpos, ypos; // current position of your ship
    private float velocityX, velocityY; // current velocity of your ship
}

Then, when you apply thrust, you change the velocity , and remember that applyThrust is only called when the thrust button is pushed, not every frame as the position update is: 然后,当您施加推力时,您可以更改速度 ,并记住仅在按下推力按钮时调用applyThrust,而不是每个帧都会调用,因为位置更新是:

void Ship::applyThrust()
{
    /* 
       Assume 1 total unit of thrust is applied each time you hit the thrust button
       so you break down the velocity into x and y components depending on the direction
       your ship is facing. 
    */    
    // thrustUnit is how much thrust each thrust button press gets you, it's arbitrary
    // and can be the 2.5 multiplier you're using in your question
    float thrustUnit = 1.0;

    // now get the x and y thrust components of velocity based on your ship's direction
    // assuming 0 degrees is pointing to the right
    float newVX, newVY = 0.0;        
    newVX = thrustUnit * cos(_angle);
    newVY = thrustUnit * sin(_angle); // radian conversion left out to save space

    // Update your ship's velocity
    updateVelocity(newVX, newVY);
}

updateVelocity will look something like: (note that the velocity is additive so it continues to drift unless a thrust is applied in the opposite direction - the same as if it were in a frictionless medium such as space) updateVelocity看起来像:(注意速度是加法的,所以它继续漂移,除非在相反的方向上施加推力 - 就像它在无间隙的介质中一样,如空间)

void Ship::updateVelocity(newVX, newVY)
{
    // update the x component
    velocityX += newVX;
    velocityY += newVY;
}

So now you'll also need an updatePosition function that takes into account your ship's velocity. 所以现在你还需要一个updatePosition函数来考虑你的船速。 This gets called with each frame update: 每次帧更新都会调用它:

void Ship::updatePosition()
{
    // add the x component of velocity to the ship's x position
    // assuming xpos and ypos are variables in the Ship class for tracking position
    xpos += velocityX;
    ypos += velocityY;
}

Now the position of the ship changes incrementally according to each velocity component at each frame update. 现在,船的位置根据每个帧更新时的每个速度分量递增地改变。 You can also make thrustUnit a member variable to allow for power-ups that can either boost or decrease your thrust component for your ship's speed and being able to control it better with a smaller thrustUnit or giving it hyperspeed with a large thrustUnit . 你也可以使用推力单元作为一个成员变量来允许加速,这可以提升或减少你的船速的推力分量,并且能够用较小的推力单位更好地控制它,或者使用大推力单位给它超速

Good luck with your game! 祝你的游戏好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM