Hey, I'm here to ask for help on a topic I'm sure a lot of people get stuck on, and that is collision detection. I'll describe what I'm having trouble with and all of the specifics below.
I'm learning Java, and after making a couple simple games I decided it would be a good learning experience to make a platformer, but I'm having a lot of trouble so far.
I'm using Java with the Slick2D library.
So far I have a character (rectangle) that you are able to control with a very basic movement system (you can move left, right, and jump.) and now I'm going into what I can't put off any longer, collision detection. I've tried a few different methods that I came up with myself, and nothing is working, I've also tried looking for some tutorials on google, but I can't find what I'm looking for.
I'm not trying to do anything fancy, I just want to be able to collide with and jump on another rectangle to create the feel of a solid block.
What is the best way of doing this? I can't seem to get the player to stop at the right place, especially while jumping onto the block.
Any and all help is appreciated, thanks for reading!
Code below:
Main class:
public class Main extends BasicGame{
Player p = new Player();
Cube c = new Cube();
public Main(String title) {
super(title);
}
public static void main(String[] args) throws SlickException{
AppGameContainer app = new AppGameContainer(new Main("Game"));
app.setTargetFrameRate(60);
app.setDisplayMode(640, 480, false);
app.start();
}
}
@Override
public void update(GameContainer gc, int delta) throws SlickException {
p.update(60);
c.update(60);
p.keyListener(gc);
}
}
Player class:
public class Player {
Rectangle p;
Cube c = new Cube();
int x, y, width, height, speed;
double gravity, yVelocity;
boolean jump, moveDown, moveLeft, moveRight, onGround, colliding;
public Player(){
x = 50;
y = 100;
width = 64;
height = 64;
speed = 100;
gravity = 500;
p = new Rectangle(x,y,width,height);
}
public void update(long deltaTime){
double delta = deltaTime / 1000.0;
if(jump)
yVelocity = -400;
if(moveLeft){
x -= speed * delta;
}
if(moveRight){
x += speed * delta;
}
First off, I haven't found any easy way of doing collision detection. I was in your position a few months ago for the first time and everyone here was a huge help, but you're going to have to program it yourself.
Here's my method (and by my, I mean the method I got from people on here).
The Simple Version
1. Find the movement vector of the object your going to move
2. Do a 'Wide Pass' to find anything that might be in its way
3. Find the first collision the object would encounter
4. find the new movement vector based on the collision
5. Apply the vector to the player's position
The Complete Version:
1. First find how far the player is going to move in both the x and y directions.
2. A wide pass is designed to quickly, and in a small number of calculations, find any object that the player might hit on its current trajectory. To do this I cast out lines from 2 or 3 of the corners of the character. Which corners I use is based on which directions the player is moving.
ie. if he's moving down and left I cast lines from the top left, bottom left and bottom right because those are the only corners that could possible hit something.
You then loop through all the objects in your scene to see which ones cross these lines.
3. Loop through the wide pass list of objects finding which one is closest to the player, as this one will be the first to be hit. I find the points at which the lines intersect the object, pick the closest one and use that to determine which collision would come first.
4. Based on that point, you can find the new movement vector.
5. Add the vectors to the current position.
I've found it easier, at least with java, to write my own class for a bound object rather than using java.awt.Rectangle, but that might just be me.
Hope this helps.
Peter
-------------------------------------
"Other than that, I have no opinion." My Blog - Check it Out
It looks like you're doing simple collision within a hardcoded rectangular area. I don't see any code for checking collisions between the player and the cube. Note that because you check for collisions before moving the player/cube, they can go too far for one frame then get yanked back.
There are many things to consider when testing for collisions. For example you may need to check the area swept out by the object from one frame to another to ensure that fast moving objects don't pass straight through thin objects. There's also the question of how to resolve the situation when multiple objects collide at once, e.g. when playing pool. There's ignoring far away objects so collision checking isn't too slow. It's a huge field. Research, and have fun.
The main thing you'll need to do is have a list of objects your player can collide with, and eveyr frame, have him check that list and see if he's collided. Realize, for platformers, you'll need to check specifically for collisions along the X-axis (to stop the player moving horizontally, while he's still moving vertically), and and Y-axis (to bump his head, or just bump his feet, if you want them to pass through the bottom of platforms).
There are a few posts about this that I've replied too that has some good info.
Okay, I've taken some of what each of you guys said, and I think I have a very very basic version of what I want, the only problem is, it's only on the X-axis.
This was in the update function of the main class by the way.
Please note that I'm only writing the collision code against one rectangle because this is just to learn the basics of collision detection.
What's happening now is the player will stop when he hits either side on the X-axis (just like I want), but if he jumps onto the block colliding on the Y-axis, he continuously bounces up and down very slightly, making my "onGround" boolean go from true to false very rapidly so the player is unable to jump. What can I do to fix this?
Also, I haven't done any collision detection on the bottom yet, but I'm assuming it will be similar to how I've done it on the X-axis.
P.S. The multiplying the velocity by 60 / 1000.0 is to simulate multiplying it by delta as I've done in the player class. Please tell me if I'm misunderstanding this whole delta thing.