Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX Rotate Image Continually with Keyboard Input  (Read 1617 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-05-22 00:27:38 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   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
1  
      Keyboard.enableRepeatEvents(true);

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

EDIT:
Here's the ship class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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;
   }
}

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline Longarmx
« Reply #1 - Posted 2013-05-22 00:29:54 »

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.

Offline Slyth2727
« Reply #2 - Posted 2013-05-22 00:35:30 »

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?

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2013-05-22 01:05:05 »

You can use
1  
yVelocity = sin(angle) * thrustPower
and
1  
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)

Offline Slyth2727
« Reply #4 - Posted 2013-05-22 01:19:01 »

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

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2013-05-22 01:27:51 »

Don't forget to throw delta in there Wink
Offline Slyth2727
« Reply #6 - Posted 2013-05-22 01:29:19 »

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

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline Otreum

Junior Member


Medals: 6



« Reply #7 - Posted 2013-07-13 14:06:34 »

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:

1  
2  
3  
4  
5  
6  
7  
         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 Smiley  (PS: I feel like a dummy right now Tongue).
Offline Slyth2727
« Reply #8 - Posted 2013-07-14 04:56:03 »

Hey I'm glad my thread is being useful Smiley. I used this code:
In my input class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
@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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
      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.

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline Otreum

Junior Member


Medals: 6



« Reply #9 - Posted 2013-07-14 05:26:16 »

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

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

And here is that video Smiley
<a href="http://www.youtube.com/v/q2As7t1n-jw?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/q2As7t1n-jw?version=3&amp;hl=en_US&amp;start=</a>
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (44 views)
2014-04-15 18:08:23

BurntPizza (40 views)
2014-04-15 03:46:01

UprightPath (56 views)
2014-04-14 17:39:50

UprightPath (38 views)
2014-04-14 17:35:47

Porlus (54 views)
2014-04-14 15:48:38

tom_mai78101 (77 views)
2014-04-10 04:04:31

BurntPizza (136 views)
2014-04-08 23:06:04

tom_mai78101 (236 views)
2014-04-05 13:34:39

trollwarrior1 (197 views)
2014-04-04 12:06:45

CJLetsGame (205 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!