2D Collision detection, need some help.

Started by
5 comments, last by Tankyroo 11 years, 8 months ago
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 render(GameContainer gc, Graphics g) throws SlickException {
p.paint(g);
c.paint(g);
}
@Override
public void init(GameContainer gc) throws SlickException {

}
@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;
}

if(x <= 0)
x=0;
if(y <= 0)
y=0;
if(x >= 640 - width)
x = 640 - width;
if(y >= 480 - width)
onGround = true;
else
onGround = false;
if(y > 480 - width)
y = 480 - width;
if(onGround && !jump)
yVelocity = 0;

y += yVelocity * delta;
yVelocity += gravity * delta;

p.setX(x);
p.setY(y);
}

public void keyListener(GameContainer gc){
Input input = gc.getInput();
if(input.isKeyPressed(Input.KEY_W) && onGround || input.isKeyPressed(Input.KEY_SPACE) && onGround){
jump = true;
}else{jump = false;}
if(input.isKeyDown(Input.KEY_A)){
moveLeft = true;
}else{moveLeft = false;}
if(input.isKeyDown(Input.KEY_D)){
moveRight = true;
}else{moveRight = false;}
}


public void paint(Graphics g){
g.drawRect(x,y,width,height);
g.drawString("X/Y:" + x + "/" + y, 10, 25);
g.drawString("onGround: " + onGround, 10, 40);
}


Cube class:

public class Cube {

int x, y, width, height;
double gravity, yVelocity;
boolean onGround;
Rectangle c;

public Cube(){
x = 300;
y = 100;
width = 64;
height = 64;
gravity = 500;
c = new Rectangle(x, y, width, height);
}

public void update(long deltaTime){
double delta = deltaTime / 1000.0;

if(y >= 480 - width){
y = 480 - width;
onGround = true;
}else{onGround = false;}

if(!onGround){
y += yVelocity * delta;
yVelocity += gravity * delta;
}

c.setX(x);
c.setY(y);
}

public void paint(Graphics g){
g.drawRect(x,y,width,height);
}
}


The code is probably very messy and I'm sure I've done a lot of things the wrong way, so feel free to point those out for my future reference.
Advertisement
Ok,

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
How would I check the collision on the lines? I've never worked with lines like that before.

Thanks for the response!
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. :)
You want to check if the player intersects with the cube. This is going to be more difficult for you as they are in separate classes.

if(cube.intersects(player)){
//DO STUFF
}

You may want to instead of having integers, change it into a rectangle:

Rectangle p = new Rectangle(x, y, width, height);

You can only use certain methods for collision with rectangles.

Hope this helped a little,
~Ben :)
Hi Tankyroo,

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.

Here and Here.

Good Luck

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thanks for the replies everyone.

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.

Here is what I have so far:


if(p.p.intersects(c.c) && p.x + p.width <= c.x + 5 && p.x + p.width >= c.x - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.x <= c.x + c.width + 5 && p.x >= c.x + c.width - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.y + p.height >= c.y - 5 && p.y + p.height <= c.y + 15){
p.y -= p.yVelocity * 60 / 1000.0;
p.yVelocity = 0.0f;
p.onGround = true;
}

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.

This topic is closed to new replies.

Advertisement