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

Author Topic: How to use "Simple Collision Detection?"  (Read 10415 times)

0 Members and 1 Guest are viewing this topic.

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
How to use "Simple Collision Detection?"
« Reply #15 on: May 02, 2011, 08:04:10 pm »
That moves it only in one direction because you are telling it to move only to one direction. :) What you want to do is to figure out which side of the rectangle is actually hit and move the object accordingly. For example, if the object is coming straight down when it hits the object, you want to move the object up to keep it above the rectangle. You should be able to figure it out by comparing the coordinates, a bit like the collision detection is done. For example to find out if the object is coming from the top, you could do something like this
Code: [Select]

if(object.x() + object.size().x > object2.x() && object.x() < object2.x() + object2.size().x && object.y() > object2.y())


Btw. sprite.move(x, y) moves the sprite horizontally the amount of x and vertically the amount of y.

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
How to use "Simple Collision Detection?"
« Reply #16 on: May 02, 2011, 08:14:29 pm »
How would that bit of code move the sprite? Also I want to make a gravity test like the ball is in a cage but the ball keeps bouncing and hitting the side of the screen what would that look like? The code?
I'm tring to learn SFML Help? Collision?

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
How to use "Simple Collision Detection?"
« Reply #17 on: May 07, 2011, 04:01:45 pm »
Quote from: "Dimple"
That moves it only in one direction because you are telling it to move only to one direction. :) What you want to do is to figure out which side of the rectangle is actually hit and move the object accordingly. For example, if the object is coming straight down when it hits the object, you want to move the object up to keep it above the rectangle. You should be able to figure it out by comparing the coordinates, a bit like the collision detection is done. For example to find out if the object is coming from the top, you could do something like this
Code: [Select]

if(object.x() + object.size().x > object2.x() && object.x() < object2.x() + object2.size().x && object.y() > object2.y())


Btw. sprite.move(x, y) moves the sprite horizontally the amount of x and vertically the amount of y.


How can i make the object collide and not move any closer and gravity? Can i make a test lets say... the screen edges is the collision detection and i make balls keep bouncing and hitting the walls?
I'm tring to learn SFML Help? Collision?

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
How to use "Simple Collision Detection?"
« Reply #18 on: May 09, 2011, 10:17:02 am »
I've been a bit busy lately so sorry for the delayed answer.

The bouncing off the sides of the screen is done in the pong example for the two of the four edges. You basically only need to copy that part and figure out the formulas for the other two if you need those, too (the collision with the those edges is there but hitting those edges just ends the game, it doesn't bounce the ball of the wall). There is a way to do it with one function, without any special cases (=only one formula would be needed). The problem is that you would need to know vector math to be able to understand it. So I ask you, do you know anything about vector math?

Oh, and checking the collision with the edge of the screen is even easier than bounding box detection. You only need to check if the ball's coordinates are less than zero or over the screen width or height to determine that the ball is colliding with the edges.

I would probably use vectors for the gravity, too (unless you only want it to keep bouncing at the same spot). I would use a vector for the speed of the ball and another vector for the gravity that keeps pulling the ball to the ground.

Girby2K11

  • Newbie
  • *
  • Posts: 24
    • View Profile
How to use "Simple Collision Detection?"
« Reply #19 on: May 09, 2011, 05:23:09 pm »
I no nothing about vector math, but I will check it out.
I'm tring to learn SFML Help? Collision?