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

Author Topic: Collision detection  (Read 2123 times)

0 Members and 1 Guest are viewing this topic.

spreyy

  • Guest
Collision detection
« on: August 19, 2013, 11:33:29 pm »
Hello.
I got problem with collision detection.
My map looks like:
1, 1, 1, 1, 2, 1, 1
1, 2, 1, 1, 1, 3, 1
1, 1, 1, 3, 1, 1, 1
1, 2, 1, 1, 1, 3, 1

Every numer is a 16x16 tile, different number - different object.
Number 1 means grass so character can move through it, numbers higher than 1 should block character.
To move character im using .move(x,y).

Can you help me with collision detection ? I got no ideas.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision detection
« Reply #1 on: August 20, 2013, 01:12:20 am »
It sounds like all you need to do is add a few if statements to check if he's trying to move into a square he shouldn't.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Collision detection
« Reply #2 on: August 20, 2013, 12:38:03 pm »
Hello.
I got problem with collision detection.
My map looks like:
1, 1, 1, 1, 2, 1, 1
1, 2, 1, 1, 1, 3, 1
1, 1, 1, 3, 1, 1, 1
1, 2, 1, 1, 1, 3, 1

Every numer is a 16x16 tile, different number - different object.
Number 1 means grass so character can move through it, numbers higher than 1 should block character.
To move character im using .move(x,y).

Can you help me with collision detection ? I got no ideas.
are you moving 16 or ? pixels per key press?
When user presses movement key check if place where he is gonna be is walid.
if walid move him, else dont
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

spreyy

  • Guest
Re: Collision detection
« Reply #3 on: August 20, 2013, 01:47:30 pm »
That's my right move function:
Quote
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)&&(posx<784))
      {
         character.move(speed * dt,.0f);
      }

The problem is that I'm not moving 16 pixels or table index.
What do you think, should I change it to move character 16 pixels or use table indexes for movement ?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision detection
« Reply #4 on: August 20, 2013, 08:44:47 pm »
Restricting you to moving exactly 16 pixels at a time is definitely overkill in this case.  The easiest solution might be to do the move() command, call a function to check for collisions between the player and any impassable terrain objects (assuming everything is a square, just use their bounding boxes) and if there is such a collision then simply move() him back.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Collision detection
« Reply #5 on: August 20, 2013, 11:19:52 pm »
yeah I agree with Ixrec, you should check collision on a move and only onto the scare your trying to move to.
You shouldn't need bounding boxes, since you have a grid map, something like this should work fine.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)&&(posx<784))
{
     sf::Vector2f oldpos = character.position;
     character.move(speed * dt,.0f);
     character.CheckMapCollision(oldpos);
}

Character::CheckMapCollision(sf::Vector2f oldPos)
{
     if(map[(int)position.x/16][(int)position.y/16] == 2)
     {
          character.position = oldpos;
     }
}
 

This is just very quick and pseudo but the idea should be there, just check where the player is going to be, if it's passable and if not, move him back. the character.position.x/16 <- this is the size of the tile and the 2 I assume is the non-passable tile. Plenty of ways to deviate from this but the best I can do from the top of my head at work :P

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision detection
« Reply #6 on: August 21, 2013, 12:47:32 am »
That's true, you probably can just divide by 16 for now.  I suggested bounding boxes since that's almost as efficient but a lot more general (in case he changes how his map works later) and is likely to prevent any chance of off-by-one and other irritating errors.

@spreyy Of course, if you're sure your map format will never change, Gobbles' approach is probably the better choice.

spreyy

  • Guest
Re: Collision detection
« Reply #7 on: August 21, 2013, 02:29:48 am »
Thanks, I'll try your ideas and report back.

Edit:
Partially it's working, but I have to round up the result of dividing, because sometimes my character walks on obstacles.


« Last Edit: August 22, 2013, 02:14:57 pm by spreyy »