• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Tankyroo

2D Collision detection, need some help.

6 posts in this topic

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:
[CODE]
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);
}
}
[/CODE]

Player class:
[CODE]
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);
}
[/CODE]

Cube class:
[CODE]
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);
}
}
[/CODE]

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

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
How would I check the collision on the lines? I've never worked with lines like that before.

Thanks for the response!
0

Share this post


Link to post
Share on other sites
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. :) Edited by jefferytitan
1

Share this post


Link to post
Share on other sites
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 :)
0

Share this post


Link to post
Share on other sites
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.

[url="http://www.gamedev.net/topic/629260-coding-jump-for-a-2d-python-game/page__view__findpost__p__4967145"]Here[/url] and [url="http://www.gamedev.net/topic/629326-going-crazy-collision-detection/"]Here[/url].

Good Luck
0

Share this post


Link to post
Share on other sites
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:

[CODE]
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;
}
[/CODE]
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. Edited by Tankyroo
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0