Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: collsion detection  (Read 2626 times)

0 Members and 1 Guest are viewing this topic.

DigiByte

  • Newbie
  • *
  • Posts: 11
    • View Profile
collsion detection
« on: December 01, 2011, 08:11:40 pm »
Hello everyone, first of all, I hope this is the right board for my question...

But here it is: I would like to have some working collision detection in my platfomer game. The collision should detect left collision, right collision, up collision and down collision. I know how I could get collision detection working on an object, but I have no idea how I could create a detection where left, right, up and down sides are separated checks.

I would really love it, if someone could help me with some explanation how I could make this kind of detection. I'm working with rectangles, so it can't be that difficult I think, but I can't get it working.

Maybe a little (pseudo - ) code would be helpful, just a little example that to explain how it works, with just a platform (big rectangle) and player (smaller square or rectangle). But (pseudo -) code isn't necessary for me, I would already be happy if I had some more explanation about what's the best way to do it.

Thank you,

DigiByte.
having some troubles installing SFML on my mac...

Phalanx

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://gameandcode.com
collsion detection
« Reply #1 on: December 01, 2011, 08:48:25 pm »
I'm a beginner, so this could be completely wrong, but here is how I would handle it. Let's assume you have a ball hitting a brick. When they collide, you want the side of the ball to flatten (by replacing the sprite) and the brick to have vibration lines coming out of the side that was hit.

Code: [Select]
BallObj
    float x
    float y
    enum direction {North, East, South, West}
    showCollision()

BrickObj
    float x;
    float y;
    showCollision(impactDirection)

CollisionService
    isColliding(obj1, obj2)


Given that rough structure, code would be:

Code: [Select]
if(isColliding(BallObj, BrickObj)) {
    BallObj.showCollision()
    BrickObj.showCollision(BallObj.direction)
}

BallObj::showCollision() {
    image = ball_north.png //if traveling north
}

BrickObj.showCollision(impactDirection) {
    image = brick_south.png //opposite of direction passed in
}


Then, each frame, maintain the direction the ball is traveling in the BallObj class by looking at the last coords before overriding with the new coords.

That example would work for 4 way directions. If you need 360 degrees, it becomes a bit more complex, but a similar approach should work. Also, if both objects are moving, they could both take the object they hit as a parameter, and check for a getDirection function off of the object. Curious to hear from the experts if this is how it would work out.
Wannabe Game Developer
Game & Code

DigiByte

  • Newbie
  • *
  • Posts: 11
    • View Profile
collsion detection
« Reply #2 on: December 02, 2011, 06:49:20 pm »
But that isn't a solution to my problem. I wanted to know how i could realize the collision like a described, in your code, i can't find any collision checks...
having some troubles installing SFML on my mac...

Phalanx

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • http://gameandcode.com
collsion detection
« Reply #3 on: December 02, 2011, 07:28:47 pm »
For that, this is a good starting point:

https://github.com/SFML/SFML/wiki/SourceSimpleCollisionDetection

If you're just dealing with squares or other simple collisions, something like that seems like the way to go. If you plan to get more complex, I believe box2d or chipmunk have collision detection, which would be a good way to go if you're going to have physics.
Wannabe Game Developer
Game & Code

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
collsion detection
« Reply #4 on: December 02, 2011, 09:23:03 pm »
You could use BoundingBox collisions, but doing a little change in boxes to check if there is collision left, right, up, down.

A pseudo-code for checking if ther is free place at a given point relative to the object:

Code: [Select]
bool collision(object, other, newX, newY)
{
    if (object.x + newX > other.x + other.width)
        return false;
    if (object.x + object.width + newX < other.x)
        return false;
    if (object.y + newY > other.y + other.height)
        return false;
    if (object.y + object.height + newY < other.y)
        return false;

    return true;
}


Checking left collision:
Code: [Select]
if ( !collision(object, other, -1, 0) )
    moveLeft();

And same for right, up and down...

Cheers

DigiByte

  • Newbie
  • *
  • Posts: 11
    • View Profile
collsion detection
« Reply #5 on: December 03, 2011, 11:43:40 am »
@ Phalanx: thanks, i'll take a look on that page. :)

@ julen26: So the function you showed as pseudo code, is for collision detection anywhere, at any side? I guess the arguments newY and newX are the places where to check if there is collision on newY and newX?

Thank you both for the replies, i'll check it tomorrow or somewhen. :)
having some troubles installing SFML on my mac...

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
collsion detection
« Reply #6 on: December 03, 2011, 01:00:17 pm »
Quote from: "DigiByte"

@ julen26: So the function you showed as pseudo code, is for collision detection anywhere, at any side? I guess the arguments newY and newX are the places where to check if there is collision on newY and newX?

Not exactly, if you remove newX and newY arguments, that code should check a simple boundingBox collision.

But those arguments move the boundingBox of the first object to a certain position relative to the object, so you cant check collision 1 pixel left, 10 pixels right ...


DigiByte

  • Newbie
  • *
  • Posts: 11
    • View Profile
collsion detection
« Reply #7 on: December 03, 2011, 02:47:05 pm »
Oh, ok. I get it:)

Thank you for the explanation with the images :)

DigiByte.
having some troubles installing SFML on my mac...

 

anything