LibGDX Rotate Image Continually with Keyboard Input

Alright I finally did it! I’m learning a gaming library, libGDX, which I especially liked cause of the particles :wink: I’m starting to love those puppies. Anyway, I am following the Dustin Riley tutorials, and I was wondering: How would I be able to, instead of having the spaceship have 8 positions (up right, up, lower right, etc) , be able to press and hold A or D and have the ship rotate accordingly as well as have it “thrust” when W is pressed. I have this:


	public boolean keyDown(int keycode) {
		ship = world.getShip();
		switch (keycode) {
		case Keys.W:
			ship.getVelocity().y += .1;
			break;
		case Keys.S:
			ship.getVelocity().y -= .1;
			break;
		case Keys.A:
			ship.getVelocity().x -= .1;
			break;
		case Keys.D:
			ship.getVelocity().x += .1;
			break;
		default:
			break;
		}
		return true;
	}

and obviously that doesn’t work for so many reasons. I don’t even know why I did it, I knew it was ridiculous. And I know about


		Keyboard.enableRepeatEvents(true);

but is there a good way to achieve what I am trying?

EDIT:
Here’s the ship class:


package com.slyth.angrymasons.Models;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;

public class Ship extends MovableEntity {

	public Ship(Vector2 position, float width, float height, float rotation, float SPEED) {
		super(SPEED, rotation, width, height, position);
	}

	public void update() {
		position.add(velocity.tmp().mul(Gdx.graphics.getDeltaTime() * SPEED));
		if (velocity.x != 0 || velocity.y != 0)
			rotation = velocity.angle() - 90;

		bounds.x = position.x;
		bounds.y = position.y;
	}
}

I would have it set a Boolean to true when you press down a key. In your ship update loop, check if this Boolean is yes, then move in increments. Sorry, I’m not at my computer right now. Otherwise I would have elaborated and posted code.

alright, got the left and right coded in, however for thrust should I do something along the lines of adding the angle of the ship to the velocity?

You can use

yVelocity = sin(angle) * thrustPower

and

xVelocity = cos(angle) * thrustPower

provided that you are measuring the angle from the horizontal.

(I think this is what you were asking, please tell me if not)

Works great! All I needed to change was to add 90 to rotation and use sinDeg and cosDeg for degrees :slight_smile: thanks

Don’t forget to throw delta in there :wink:

Good point ;D
Seriously? I say rotation += .1f * rotationSpeed * Gdx.graphics.getDeltaTime() and I get no movement nor any rotation. Why is my delta returning 0?

Sorry to revive a slightly old thread, but I have been following the same tutorials and face the same issue.

I too would like to be able to rotate the sprite left or right by holding down A or D, but so far, I haven’t had much success in trying to get it to work.

I have made 2 boolean variables called AisPressed and DisPressed, setting them with default values of false.
I have 2 getter methods inside of the InputHandler class which return the value of AisPressed or DisPressed.
When an either A or D is pressed, it changes the boolean value of AisPressed or DisPressed to true. When the key is lifted, it changes to false again.
I have code inside of the Ship.update method which asks:

			if(input.APressed(true)){
				rotation = velocity.angle() - 1;
			};

			if(input.DPressed(true)){
				rotation = velocity.angle() + 1;
			};

However, I believe my problem is that the World.update method runs, and inside that method, the ship.update method runs before the program gets the user input, so the variable AisPressed or DisPressed is non existent just yet, therefore I get an exception error.

I have run into this issue before and have forgotten how I got around it, I am having a gigantic mental blank, so any help would be appreciated :slight_smile: (PS: I feel like a dummy right now :P).

Hey I’m glad my thread is being useful :). I used this code:
In my input class:


@Override
	public boolean keyDown(int keycode) {
		ship = world.getShip();

		switch (keycode) {
		case Keys.W:
			ship.isKeyDown = true;
			ship.up = true;
			break;
		case Keys.S:
			ship.isKeyDown = true;
			ship.down = true;
			break;
		case Keys.A:
			ship.left = true;
			break;
		case Keys.D:
			ship.right = true;
			break;
		case Keys.UP:
			ship.isKeyDown = true;
			ship.up = true;
			break;
		case Keys.DOWN:
			ship.isKeyDown = true;
			ship.down = true;
			break;
		case Keys.LEFT:
			ship.left = true;
			break;
		case Keys.RIGHT:
			ship.right = true;
			break;
		default:
			break;
		}

		return true;
	}

	@Override
	public boolean keyUp(int keycode) {
		ship = world.getShip();

		switch (keycode) {
		case Keys.W:
			ship.isKeyDown = false;
			ship.up = false;
			break;
		case Keys.S:
			ship.isKeyDown = false;
			ship.down = false;
			break;
		case Keys.A:
			ship.left = false;
			break;
		case Keys.D:
			ship.right = false;
			break;
		case Keys.UP:
			ship.isKeyDown = false;
			ship.up = false;
			break;
		case Keys.DOWN:
			ship.isKeyDown = false;
			ship.down = false;
			break;
		case Keys.LEFT:
			ship.left = false;
			break;
		case Keys.RIGHT:
			ship.right = false;
			break;
		default:
			break;
		}

		return true;
	}

and in my ship class:


		if (left) {
			if (rotation <= 360) {
				rot = .1f;
			} else {
				rotation = 0;
				rot = .1f;
			}

		} else if (right) {
			if (rotation >= 0) {
				rot = -.1f;
			} else {
				rotation = 360;
				rot = -.1f;
			}
		} else if (!right && !left) {
			rot *= .94f;
		}
		rotation += rot * rotateSpeed;

		if (up) {
			float yP = MathUtils.sinDeg(rotation + 90) * SPEED * deg;
			float xP = MathUtils.cosDeg(rotation + 90) * SPEED * deg;

			velocity.y = yP;
			velocity.x = xP;

			if (deg <= 1) {
				deg += .1;
			}
		} else if (!up && !down) {
			velocity.x *= velocitySlow;
			velocity.y *= velocitySlow;
			deg *= .98;
		} else if (down) {
			float yP = MathUtils.sinDeg(rotation + 90) * SPEED * deg;
			float xP = MathUtils.cosDeg(rotation + 90) * SPEED * deg;

			velocity.y = yP;
			velocity.x = xP;

			if (deg >= -1) {
				deg -= .1;
			}
		}
		if (!isKeyDown) {

		}
		position.add(velocity.tmp().mul(Gdx.graphics.getDeltaTime() * SPEED));

this is with the extra stuff to make the turning slow down after you stop pressing the key and I am sure there is a better way to do this, but there you go.

Awesome stuff, thankyou, I’ll try it out and see how it goes.

I have also been experimenting with getting the player sprite to rotate based on where the mouse is.

Currently the keyboard rotation is for vehicles in the game i’m working on, such as a ship or catapult. Mouse influenced rotation will be for when running around.

I will post what I have done in order to get these working, when or if I get them working. :stuck_out_tongue:

Once again thankyou.

UPDATE: So I have the ship rotating and moving forward and backwards, after some guess work and fiddling around, now I have to adjust the variables so that there is some kind of slow down to the rotation speed and velocity, which I guess is where rotateSpeed and velocitySlow come into play. I haven’t really fine tuned variable values just yet, but just out of curiousity, what have you set the following variables values as, and what type of variables are they (float? int?)?
-rotateSpeed
-deg
-velocitySlow
-SPEED

UPDATE 2:
HOORAY! So I stumbled and fumbled around with the code, which was great as I understood it the more I struggled to understand it :stuck_out_tongue:

I now have basic smooth movement and have a video on the way :slight_smile:

And here is that video :slight_smile:

q2As7t1n-jw