Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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 532 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:

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

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!
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.
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
1  
2  
3  
4  
5  
6  
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.
1  
2  
3  
4  
5  
6  
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  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Grunnt (13 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!