Physics like asteroides

Posted by user2933016 on Game Development See other posts from Game Development or by user2933016
Published on 2014-04-19T09:19:10Z Indexed on 2014/05/26 22:07 UTC
Read the original article Hit count: 319

Filed under:

I try to make a ship that has the physic properties like asteroides. I have this for now(All in Java):

Ship.class

public class Ship
{
    public static final float sMaxHealth            = 0.1F;
    public static final float sMaxMoveVelocity      = 5.0F;
    public static final float sMaxAngleVelocity     = 20.0F;
    public static final float sRadius               = 1.0F;
    public static final float sMoveDeceleration     = 10.0F;
    public static final float sMoveAcceleration     = 2.0F;
    public static final float sAngleDeceleration    = 15.0F;
    public static final float sAngleAcceleration    = 20.0F;


    private float mHealth;
    private float mXVelocity;
    private float mYVelocity;
    private float mAngleVelocity;

    private float mX;
    private float mY;
    private float mAngle;
}

(I let the getter and setter away for now)

Controller code

        // Player input
        if(Gdx.input.isKeyPressed(Keys.UP))
        {
            mPlayer.setXVelocity(mPlayer.getXVelocity() + (float) Math.cos(mPlayer.getAngle()) * Ship.sMoveAcceleration);
            mPlayer.setYVelocity(mPlayer.getYVelocity() + (float) Math.sin(mPlayer.getAngle()) * Ship.sMoveAcceleration);
        }
        if(Gdx.input.isKeyPressed(Keys.LEFT))
        {
            mPlayer.setAngleVelocity(mPlayer.getAngleVelocity() + Ship.sAngleAcceleration * pDeltaTime);
        }
        if(Gdx.input.isKeyPressed(Keys.RIGHT))
        {
            mPlayer.setAngleVelocity(mPlayer.getAngleVelocity() - Ship.sAngleAcceleration * pDeltaTime);
        }

        // X velocity
        if(mPlayer.getXVelocity() < 0)
        {
            if(-mPlayer.getXVelocity() > Ship.sMaxMoveVelocity)
            {
                mPlayer.setXVelocity(-Ship.sMaxMoveVelocity);
            }
            mPlayer.setXVelocity(mPlayer.getXVelocity() + Ship.sMoveDeceleration * pDeltaTime);
            if(mPlayer.getXVelocity() > 0)
            {
                mPlayer.setXVelocity(0);
            }
        }
        else
        if(mPlayer.getXVelocity() > 0)
        {
            if(mPlayer.getXVelocity() > Ship.sMaxMoveVelocity)
            {
                mPlayer.setXVelocity(Ship.sMaxMoveVelocity);
            }
            mPlayer.setXVelocity(mPlayer.getXVelocity() - Ship.sMoveDeceleration * pDeltaTime);
            if(mPlayer.getXVelocity() < 0)
            {
                mPlayer.setXVelocity(0);
            }
        }

        // Y velocity
        if(mPlayer.getYVelocity() < 0)
        {
            if(-mPlayer.getYVelocity() > Ship.sMaxMoveVelocity)
            {
                mPlayer.setYVelocity(-Ship.sMaxMoveVelocity);
            }
            mPlayer.setYVelocity(mPlayer.getYVelocity() + Ship.sMoveDeceleration * pDeltaTime);
            if(mPlayer.getYVelocity() > 0)
            {
                mPlayer.setYVelocity(0);
            }
        }
        else
        if(mPlayer.getYVelocity() > 0)
        {
            if(mPlayer.getYVelocity() > Ship.sMaxMoveVelocity)
            {
                mPlayer.setYVelocity(Ship.sMaxMoveVelocity);
            }
            mPlayer.setYVelocity(mPlayer.getYVelocity() - Ship.sMoveDeceleration * pDeltaTime);
            if(mPlayer.getYVelocity() < 0)
            {
                mPlayer.setYVelocity(0);
            }
        }

        // Angle velocity
        if(mPlayer.getAngleVelocity() < 0)
        {
            if(-mPlayer.getAngleVelocity() > Ship.sMaxAngleVelocity)
            {
                mPlayer.setAngleVelocity(-Ship.sMaxAngleVelocity);
            }
            mPlayer.setAngleVelocity(mPlayer.getAngleVelocity() + Ship.sAngleDeceleration * pDeltaTime);
            if(mPlayer.getAngleVelocity() > 0)
            {
                mPlayer.setAngleVelocity(0);
            }
        }
        else
        if(mPlayer.getAngleVelocity() > 0)
        {
            if(mPlayer.getAngleVelocity() > Ship.sMaxAngleVelocity)
            {
                mPlayer.setAngleVelocity(Ship.sMaxAngleVelocity);
            }
            mPlayer.setAngleVelocity(mPlayer.getAngleVelocity() - Ship.sAngleDeceleration * pDeltaTime);
            if(mPlayer.getAngleVelocity() < 0)
            {
                mPlayer.setAngleVelocity(0);
            }
        }

        mPlayer.setX(mPlayer.getX() + mPlayer.getXVelocity() * pDeltaTime);
        mPlayer.setY(mPlayer.getY() + mPlayer.getYVelocity() * pDeltaTime);
        mPlayer.setAngle(mPlayer.getAngle() + mPlayer.getAngleVelocity() * pDeltaTime);

Why the ship does not behave like in asteroides ? What do I wrong?

© Game Development or respective owner

Related posts about java