[SOLVED]How can i move a sprite / a tank in screen with rotation?

Hello guys. Im trying to make a tank move in angles in my screen.
Im forgot the math to do it.

The tank can rotate in the origin and by pressing W or S , the tank should go forward to that direction or backwards.

Help

public void update() {
        
        float x = getX();
        float y = getY();
        float a = 1;
        float b = 1;
        
        //Sprite.getRotation return : the rotation of the sprite in degrees
        a = sprite.getRotation() * MathUtils.degreesToRadians;
        
        b = y - x * a;

        //y = arctan * x + b
        if (pressingRight) {
            sprite.rotate(-5);
        }
        if (pressingLeft) {
            sprite.rotate(5);
        }
        
        if (pressingUp) {
            sprite.setX(x + 3);
            sprite.setY(a * (getX() + 3) + b);
        }
        
        if (pressingDown) {
            sprite.setX(x - 3);
            sprite.setY(a * (getX() - 3) + b);
        }
        
    }

xVel = sin(theta) * speed; // Sometimes -sin(theta) * speed, depending on the coordinate system
yVel = cos(theta) * speed;

Works fine :slight_smile:
Thanks Heroes!

public void update() {

        float a = 1;

        //Sprite.getRotation return : the rotation of the sprite in degrees
        a = sprite.getRotation() * MathUtils.degreesToRadians;

        //y = arctan * x + b
        if (pressingRight) {
            sprite.rotate(-5);
        }
        if (pressingLeft) {
            sprite.rotate(5);
        }

        if (pressingUp) {
            sprite.setX(getX() + MathUtils.sin(-a) * speed);
            sprite.setY(getY() + MathUtils.cos(-a) * speed);
        }

        if (pressingDown) {
            sprite.setX(getX() + MathUtils.sin(-a) * -speed);
            sprite.setY(getY() + MathUtils.cos(-a) * -speed);
        }

    }

Working.What you guys think of it?

 public void update() {

        float a = 1;

        a = sprite.getRotation() * MathUtils.degreesToRadians;

        if (pressingRight) {
            sprite.rotate(-5);
        }
        if (pressingLeft) {
            sprite.rotate(5);
        }

        if (pressingUp) {
            velocity.x += acceleration.x;
            velocity.y += acceleration.y;
        }

        if (pressingDown) {
            velocity.x -= (acceleration.x);
            velocity.y -= (acceleration.y);
        }

        //Friction
        if (!pressingUp && !pressingDown) {
            if (velocity.x > 0) {
                velocity.x -= acceleration.x;
                if (velocity.x < 0) {
                    velocity.x = 0;
                }
            }

            if (velocity.x < 0) {
                velocity.x += acceleration.x;
                if (velocity.x > 0) {
                    velocity.x = 0;
                }
            }

            //Y
            if (velocity.y > 0) {
                velocity.y -= acceleration.y;
                if (velocity.y < 0) {
                    velocity.y = 0;
                }

            }

            if (velocity.y < 0) {
                velocity.y += acceleration.y;
                if (velocity.y > 0) {
                    velocity.y = 0;
                }
            }

        }
        //End of Friction
        
        
        //Limit velocity.y
        if (velocity.y > topSpeed.y) {
            velocity.y = topSpeed.y;
        } else if (velocity.y < -topSpeed.y) {
            velocity.y = -topSpeed.y;
        }

        //Limit velocity.x
        if (velocity.x > topSpeed.x) {
            velocity.x = topSpeed.x;
        } else if (velocity.x < -topSpeed.x) {
            velocity.x = -topSpeed.x;
        }

        sprite.setX(getX() + MathUtils.sin(-a) * velocity.x);
        sprite.setY(getY() + MathUtils.cos(-a) * velocity.y);

    }

Sorry to bother again, but im trying to make a Ballistic class for bullets…
Not working properly, can you help me?
I think its the way i calculated the angle.


package br.lopes.singleplayer.levels.Weapons;

import com.badlogic.gdx.math.MathUtils;

/**
 * André V Lopes
 */
public class Ballistic {

    private double atan;
    private float dx, dy;

    public Ballistic(float x0, float y0) {
        dx = x0;
        dy = y0;
    }

    public void calculateAngle(double xTank, double xClick, double yTank, double yClick) {

        if (xClick < xTank && yClick < yTank) {

            atan = Math.PI + (Math.atan((yTank - yClick) / (xTank - xClick)));

        } else if (xClick >= xTank && yClick >= yTank) {

            atan = Math.atan((yTank - yClick) / (xTank - xClick));
        } else if (xClick < xTank && yClick >= yTank) {
            atan = Math.PI - (Math.atan((yClick - yTank) / (xTank - xClick)));

        } else if (xClick > xTank && yClick < yTank) {
            atan = (2 * Math.PI) - (Math.atan((yClick - yTank) / (xTank - xClick)));

        } else {
            atan = 0;

        }

    }

    public float getX(float velocity, float delta) {

        dx = dx + MathUtils.sin((float) -atan) * velocity;

        return dx;
    }

    public float getY(float velocity, float delta) {

        dy = dy + MathUtils.cos((float) -atan) * velocity;

        return dy;
    }

}



float dx = xClick-xTank;
float dy = yClick-yTank;
atan = Math.atan2(dy, dx);

The angle is messed up.
Depending on which quadrant i fire, it will mess up the angle.

package br.lopes.singleplayer.levels.Weapons;

import com.badlogic.gdx.math.MathUtils;

/**
 * André V Lopes
 */
public class Ballistic {

    private float atan;
    private float dx, dy;

    public Ballistic(float x0, float y0) {
        dx = x0;
        dy = y0;
    }

    public void calculateAngle(float xTank, float xClick, float yTank, float yClick) {

        atan = MathUtils.atan2((yClick - yTank), (xClick - xTank));
        //atan = atan - MathUtils.PI/2;
    }

    public float getX(float velocity, float delta) {

        dx = (dx + MathUtils.sin(atan) * velocity);

        return dx;
    }

    public float getY(float velocity, float delta) {

        dy = (dy + MathUtils.cos(atan) * velocity);

        return dy;
    }

}

I tried this , same problem…

package br.lopes.singleplayer.levels.Weapons;

import com.badlogic.gdx.math.MathUtils;

/**
 * André V Lopes
 */
public class Ballistic {

    private float atan;
    private float dx, dy;

    public Ballistic(float x0, float y0) {
        dx = x0;
        dy = y0;
    }

    public void calculateAngle(float xTank, float xClick, float yTank, float yClick) {

        if (xClick < xTank && yClick < yTank) {

            atan = MathUtils.PI + MathUtils.atan2((yClick - yTank), (xClick - xTank));

        } else if (xClick >= xTank && yClick >= yTank) {

            atan = MathUtils.atan2((yClick - yTank), (xClick - xTank));
        } else if (xClick < xTank && yClick >= yTank) {
            atan = MathUtils.PI - MathUtils.atan2((yClick - yTank), (xClick - xTank));

        } else if (xClick > xTank && yClick < yTank) {
            atan = (2 * MathUtils.PI) - MathUtils.atan2((yClick - yTank), (xClick - xTank));

        } else {
            atan = MathUtils.atan2((yClick - yTank), (xClick - xTank));
        }
    }

    public float getX(float velocity, float delta) {

        dx = (dx + MathUtils.sin(atan) * velocity);

        return dx;
    }

    public float getY(float velocity, float delta) {

        dy = (dy + MathUtils.cos(atan) * velocity);

        return dy;
    }

}

I got it working.

public float calculateAngle(float xTank, float xClick, float yTank, float yClick) {

        atan = (float) Math.atan2(yClick - yTank, xClick - xTank);
        atan = (float) (atan - Math.PI / 2);
        return atan;
    }