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.
What do you mean? Acceleration? Input? Be more specific please.
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. Anyways, I hope this helps!
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.
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.
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.
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.
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;
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.
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.
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…
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.
That’s disheartening, but I suppose part of the process. Thanks for trying to help and being patient.
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.
Last reply. You were right about me not knowing what I was learning. Would you recommend LWJGL, Slick2d or Libgdx personally?
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.