Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (581)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
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  
  I'm using slick2d and lwjgl and need suggestions on how to code truck movement  (Read 946 times)
0 Members and 1 Guest are viewing this topic.
Offline xabe13

Senior Newbie

« Posted 2014-05-04 20:38:38 »

I'm trying to create a game where my truck drives around and picks up different equipment to deliver.  I have the truck graphic drawn but need pointed in the right direction to actually get truck/vehicle-like movement from the keyboard.  I have been lurking and decided to just create an account to ask this question.  Any help would be highly appreciated.
Offline Nausica
« Reply #1 - Posted 2014-05-04 21:39:13 »

What do you mean? Acceleration? Input? Be more specific please.
Offline Jacob Pickens
« Reply #2 - Posted 2014-05-04 21:50:48 »

Hmmm, this is quite tricky in my opinion. Very Tricky indeed. Doing this in slick would be quite a task in my opinion but LWJGL could handle it. I assume you are using slick. Maybe try something like tweening:


int x = 0, y = 0;

int targetX = 0;
int targetY = 0;

int rotation = 0;

public void render() {
     truckSprite.setCenterOfRotation(truckSprite.getWidth() * scale / 2, truckSprite.getHeight() * scale * / 2);

     truckSprite.setRotation((float)Math.toRadians(rotation)); //When you want precise calculations always cast to float.

     truckSprite.draw(x, y, 1); //X, Y, SCALE (Set scale to one if you don't want any scaling)

public void update(Input input) {
     x = (targetX - x) * .1; //Probably want to fiddle with * .1 on this.
     y = (targetY - y) * .1;

     if(input.isKeyDown(Input.KEY_W)) {
          targetY = y - 30; //You might want to tweak thirty on all these targets a little bit because it kinda involves speed and such
     if(input.isKeyDown(Input.KEY_A)) {
          targetX = x - 30;
          rotation -= 5; //Speed the car turns
     if(input.isKeyDown(Input.KEY_S)) {
          targetY = y + 30;
     if(input.isKeyDown(Input.KEY_D)) {
          targetX = x + 30;
          rotation += 5; //Speed the car turns


Something like that. Though the turning of the car might make it a little hard to control. Tongue Anyways, I hope this helps!

Live development of whatever I feel like making!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xabe13

Senior Newbie

« Reply #3 - Posted 2014-05-04 22:53:51 »

I'm sorry. Yes, acceleration. I THINK I'm using both. Maybe not, just slick. I've went through tutorials and used certain things from both.  I don't need anything too crazy, as I'll expand on it later if need be.
Offline xabe13

Senior Newbie

« Reply #4 - Posted 2014-05-04 22:55:36 »

I think I replied to the wrong one. Anyway, I'm not 100% on where to put the code, as I've got a Main class, an options class, a credits class, a level one class, and a menu class.
Offline Jacob Pickens
« Reply #5 - Posted 2014-05-04 23:01:58 »

This code would be put into your player/truck class and stuffs. Before you do anything too advanced make sure to study up on your library and basic game setups and such and such.

Live development of whatever I feel like making!
Offline xabe13

Senior Newbie

« Reply #6 - Posted 2014-05-04 23:17:18 »

I understand. Thanks. I know I need to study up on everything. What I'm mostly doing is trying to drum up excitement from a more advanced developer in order to talk him into doing this with me. I don't actually have a truck class. In the tutorial, they showed drawing it and everything IN the level class. I'm sure that's probably wrong in so many ways.
Offline Longarmx
« Reply #7 - Posted 2014-05-04 23:56:55 »

So you're trying to make a game, without knowing how it works, to impress another developer? It sound's like you don't really know the language very well yet.
What tutorial do you keep referencing? You sound like you don't even know what you are learning.

My suggestion: Stop now and learn the language. When you are comfortable with Java, then you can begin to learn game development. Sorry if this post sound derogatory but it will probably be the best for you to take a step back and start from the beginning.

@Jacob Pickens If I understand the code correctly, that is not a good way to rotate and move an object because you will need those "magic numbers" to keep your car's rotation and direction the same. My advice would be to use vectors or at least some trig. With vectors you can do
float speed = 5;
Vector vector = new Vector(0, speed); // Speed is five units vertical
vector.rotate(Math.toRadians(45)); //rotate 45 degrees clockwise so you're traveling up and to the right with a slope of 1

x += vector.x; // update position vars
y += vector.y;

Using trig is just as easy.
float angle = 0;
float speed = 5;

angle += 45; // Again, rotate clockwise 45 degrees.
x += Math.sin(Math.toRadians(angle)) * speed; // Update position vars
y += Math.cos(Math.toRadians(angle)) * speed;

Offline xabe13

Senior Newbie

« Reply #8 - Posted 2014-05-05 00:28:49 »

I don't know the language when using it for gaming.  I have a decent amount of basic knowledge. I know why things are doing what they do, I just don't know for sure how to set things up to do them.  This other developer is my friend and co-worker and he has a very extensive amount of knowledge in Java. He actually has a BA in CS and I've been trying to get him to build something with me.  Through my Java classes, he's been more or less my tutor as well. 
Offline Longarmx
« Reply #9 - Posted 2014-05-05 00:42:35 »

Ok. That sounds more legitimate. I originally thought that you were trying to suck up to someone in order to get your share of a game that they mainly developed like story, graphics, even profits. Sorry if I was rude. Continue learning and then one day make a game.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xabe13

Senior Newbie

« Reply #10 - Posted 2014-05-05 00:49:41 »

I completely understand.  It was more of a thing where we both kind of wanted an idea in order to develop one together.  I have about 100% more motivation to do it, but sadly lack the complete know-how to do it.  I have a very general knowledge and I really only started doing this MAYBE 2-3 weeks ago.  Mostly tutorials on Slick2d and a little LWJGL.  I have a feeling that I'm going to have to work a lot harder and learn a lot more before I can even think about creating something...
Offline Longarmx
« Reply #11 - Posted 2014-05-05 00:54:31 »

A lot of people don't think of learning being a big part, but it is. I was learning for about 4 months before I was able to develop my first game. (I was still reliant on a lot of code that I had gotten from tutorials). It was probably a year or more before I could easily make a playable game on my own.

Offline xabe13

Senior Newbie

« Reply #12 - Posted 2014-05-05 00:56:42 »

That's disheartening, but I suppose part of the process.  Thanks for trying to help and being patient.
Offline Longarmx
« Reply #13 - Posted 2014-05-05 01:00:34 »

I'm sure that you could easily create a game a lot sooner if you stick with it. My learning was rather inconsistent and tutor-less.

Offline xabe13

Senior Newbie

« Reply #14 - Posted 2014-05-05 01:12:05 »

Last reply.  You were right about me not knowing what I was learning.  Would you recommend LWJGL, Slick2d or Libgdx personally?
Offline Longarmx
« Reply #15 - Posted 2014-05-05 01:21:12 »

I think that it comes down to personal preference.

  • LWJGL requires a lot of low level code. The advantage to this is that you can customize how your code base is set up. Recommended
  • Slick2D is not maintained any more I think. I haven't used it personally so I can't speak much for it.
  • Libgdx is a great engine built on top of LWJGL (or JOGL). It basically has all that low level code that you would need to write anyway. Recommended
  • JMonkeyEngine is a high level engine that allows you to create a game using a lot of code that was already created for you. I haven't used it so I again can't tell you too much about it.
  • Java2D is not necessarily the best to be using for games but it is pretty easy to use. Recommended for learning.

I have only mainly used LWJGL and Libgdx to create games and I loved them. (Check out my projects if you want to get an idea of each. Both projects have source code that you can look through. Both libraries have their ups and downs.

This type of question is highly opinionated though and I'm sure that somebody else will want to tell you what they think about other libraries.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

MrMapcom (13 views)
2015-05-23 20:26:16

MrMapcom (19 views)
2015-05-23 20:23:34

Waterwolf (29 views)
2015-05-20 15:01:45

chrislo27 (35 views)
2015-05-20 03:42:21

BurntPizza (70 views)
2015-05-10 15:53:18

FrozenShade (55 views)
2015-05-07 09:11:21

TheLopais (218 views)
2015-05-06 13:36:48

TheLopais (202 views)
2015-05-06 13:35:14

TheLopais (206 views)
2015-05-06 13:33:39

TheLopais (226 views)
2015-05-06 13:32:48
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 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‑
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!