collision issues - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: collision issues (/thread-8593.html)
collision issues - markhula - Feb 1, 2011 07:37 AM
I'm going round in circles on this!
Basically I need to know what side a rectangle intersects another at (2d).
The problem though is more tricky that that for the player moving diagonals as I wish him to 'slide' over if the direction requested can't be moved in (e.g. diagonal up/right; hit's wall up but continues right).
I have this kind of working but have issue if say there is a small 'gap' in the wall (i.e. it's actually 2 separate objects). I still wish to continue moving right but I have of course also hit the 2nd object which will interpret the intersection on it's side rather than it's base (which is of course correct!). So my player then stops i.e. gets stuck on the corner because of the diagonal.
Not a very clear explanation I know!; but anyone doing a 2d scroller will get the issues :-)
Any help appreciated!
RE: collision issues - markhula - Feb 1, 2011 07:55 AM
Funny isn't it.
Been trying to sort this for days, googling and reading.
Do a post and then "think" I have solved it.
Anyone else's input still appreciated though!
Here's what I *think* works; but needs more testing.
Collision has occurred.
Unwind the collision literally 1 pixel at a time until there is no collision. i.e. go back one 'x', check for collision, then go back 1 'y', check for collision etc.
Eventually you will be collision free!
Then it's just a case of checking a 1 pixel inc in say 'x'; if a collision results then move opposite way (remember we are talking only about diagonals) else movement must be in x.
Hope that makes sense or helps someone else!
Fingers crossed it's a 100% reliable method! :-))
RE: collision issues - Skorche - Feb 1, 2011 09:06 AM
It sounds like you are looking for the minimum separating distance and normal. For non-rotating rectangles this is really easy. Just find the distance in each direction (up, down, left, right) that you need to move your objects in order to push them apart. Then push them apart by that distance in that direction.
Applying this "push" is simple if you are only colliding moving to non-moving objects. Between moving objects, you have to do it based on mass. Multiply the distance applied to the first object by (1/m1)/(1/m1 + 1/m2) and the distance applied to the second by -(1/m2)/(1/m1 + 1/m2) where m1 and m2 are the masses of the objects. The second distance is negative because you need to push them in opposite directions.
The following site has a nice interactive animation to play with a bit down on the page. It should clear some stuff up hopefully.
The method you posted above will work, but will be much slower and more complicated to implement.
RE: collision issues - markhula - Feb 1, 2011 09:48 AM
I was hoping you'd get in on the action. :-)
The real issue I have is that effectively I can collide with 2 objects simultaneously one will return a collide_bottom the other collide_left hence the 'stuck on edges' between objects. (where there is a gap but not large enough for the player to go down).
I shall check out the links you suggest though.
p.s. ok now I'm lost! :-)
I guess I am doing something similar without realising it. But the collision with 2 objects causes a problem. Sliding along the top of 2 separate objects beneath me when doing diagonal down & right for example means I will hit the left edge of the 2nd object eventually; which causes me to stop moving though I should slide over it because the collision with the previous object of course was hitting the top.
How do i resolve this?, as it depends on which object gets processed first.........
RE: collision issues - markhula - Feb 1, 2011 10:05 AM
Should I give up what I am trying to do and just use Chipmunk! :-))))
Define my player as a circle, to avoid nasty corner issues and 'somehow' add my tile scenery as rectangles......
Will it all be a nightmare (not because of Chipmunk you understand!!); just trying to do an arcade game where the physic's doesn't prose problems and cripple fps.
RE: collision issues - ThemsAllTook - Feb 2, 2011 09:15 AM
(Feb 1, 2011 09:48 AM)markhula Wrote: I guess I am doing something similar without realising it. But the collision with 2 objects causes a problem. Sliding along the top of 2 separate objects beneath me when doing diagonal down & right for example means I will hit the left edge of the 2nd object eventually; which causes me to stop moving though I should slide over it because the collision with the previous object of course was hitting the top.
I remember dealing with this in Water Tower. The way I fixed it was to sort my collisions so that closer objects were tested before farther ones. Since a block directly below your character will always be closer than one below and a bit ahead, this should solve the problem you're having. Now, in my case everything was the same size, so sorting by distance between the centers of objects was enough, but if you have different sized objects you might need to sort more intelligently somehow.
RE: collision issues - markhula - Feb 2, 2011 09:21 AM
Hmmmm. Well my objects are added in a linked list and then collision checked as processed.
I get what you are saying though. Only checking for a collision once on the nearest block sounds like a plan; but kind of duffs up my linked list stuff. Suppose I could search the list for the 2 colliding objects too see which is nearest. They can be any size by the way! :-(((
Must be a neater method somehow!