Directional Collision Dectection

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Directional Collision Dectection

Post by Refpeuk »

I'm trying to make a platformer using love.physics but one of the things I can't seem to get past is figuring out which direction a collision is coming from. I want a jumpallowed variable to be set to 1 whenever a player's feet are on the ground but not every time he collides with a wall. It would then be unset whenever his feet left the ground. I thought the best way would be to use a sensor collision object but can't figure out how to poll it. The box2D documentation says to use something like this:

bool touching = sensorContact->IsTouching();

but a) that isn't lua, and b) searching for istouching or contactlistener (an alternative I found through google) turns up nothing on the love2d wiki.

Basically I have two questions: how do I use sensors?

Also, is there a better way for me to achieve what I'm trying to do?

Thanks!
It was the best of times, it was the worst of times . . .
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Directional Collision Dectection

Post by MarekkPie »

Haven't used love.physics, but if it uses SAT collisions (which I bet it does) then there should be a way to view the resultant normal between the two colliding objects.

Ahh, here it is:
http://love2d.org/wiki/Contact:getNormal

I assume that does what I am describing above. This will point from one object to another (I think usually from the objectB to objectA) in a unit vector. From there you can figure out the direction it came from.
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: Directional Collision Dectection

Post by Refpeuk »

But if I check the normal to see if the colliding shape is beneath or beside the playerbody, it wouldn't always reveal the same thing. For example, if I simply checked the average direction by using a rotated cartesian plane (90 degree pie slices in the 4 cardinal directions) it would still be fooled if the playerbody was at the edge of a long floor, hitting the side near the top of a tall wall, etc . . .

I would really love to know how sensors work, since I'm sure I'll need them a ton for game event triggers anyway.

Thanks for your reply - I could try it but it seems like a short term solution for no love2d documentation on how to actually use triggers.
It was the best of times, it was the worst of times . . .
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Directional Collision Dectection

Post by MarekkPie »

Well, couldn't a short term solution for the sensor be to check if the normal exists? It is calculated only when two bodies collide, so if it doesn't exist, there's no collision.

Again, haven't used love.physics, so maybe there's a better trick.
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: Directional Collision Dectection

Post by Refpeuk »

I already know how to check if there was a collision; I was just using callbacks for that. I want to be able to differentiate between him simply hitting something and his feet touching something so that I only re-allow the jump function whenever he has actually landed on the ground, not every time he collides with a wall.
It was the best of times, it was the worst of times . . .
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Directional Collision Dectection

Post by tentus »

In my physics based platformer I just compared coordinates to achieve that effect. Not terribly elegant, but it worked.
Kurosuke needs beta testers
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Directional Collision Dectection

Post by MarekkPie »

Refpeuk wrote:But if I check the normal to see if the colliding shape is beneath or beside the playerbody, it wouldn't always reveal the same thing. For example, if I simply checked the average direction by using a rotated cartesian plane (90 degree pie slices in the 4 cardinal directions) it would still be fooled if the playerbody was at the edge of a long floor, hitting the side near the top of a tall wall, etc . . .

I would really love to know how sensors work, since I'm sure I'll need them a ton for game event triggers anyway.

Thanks for your reply - I could try it but it seems like a short term solution for no love2d documentation on how to actually use triggers.
Is this what you mean?
r5Wax.jpg
r5Wax.jpg (6.38 KiB) Viewed 319 times
I always get confused by the terminology, but when I read normal, I read perpendicular. If you are "normalizing" a vector, you are making it a unit vector. If getNormal() returns a perpendicular vector, then the problem is pretty simple. Regardless of the direction you are hitting, the direction of the normal vector will be perpendicular to the surface you are colliding with. Just define a range of deviation for your x-coordinate (if you have sloped inclines that you still want to be able to jump off) and if the normal falls within that defivation, toggle jumpallowed on.

If it means get the normalized vector, then I'd do something like what tentus is decribing. I am having a hard time visualizing what to do here, but gimme some time and I might come up with something. I have a feeling though it means perpendicular, and you should be able to do what I described.

If I am completely misunderstanding your question...then...well...(/dance)
Neolitik
Citizen
Posts: 55
Joined: Sun Jun 28, 2009 3:13 pm

Re: Directional Collision Dectection

Post by Neolitik »

Me , i'm use physic sensor sensor under the Character and collision callback !

Look in my Code. , sorry not commented it's just a test for make game.
Attachments
Plateforme.love
Prototype
(16.79 KiB) Downloaded 152 times
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: Directional Collision Dectection

Post by Refpeuk »

Thanks for your replys!

MarekkPie, I'm pretty sure it's a combined normal (almost always diagonal), not a single normal of one of the surfaces, which would mean (I'm pretty sure, but I could just be missing something terribly obvious) that the normal is of no use in judging exactly where the contact itself is coming from, only where the centers of the colliding bodies are in relation to each other.

Neolitik, I'll probably use something like your's and just check each callback to see if it involves the sensor and reset the jumpallowed if it does. I was just hoping love had an implementation of box2d's contact listener functions. Also thanks for sending me your code, it's hard to read since the only comments are code you decided to scrap, but still helped show what you ment.

Thanks so much, if anyone knows if there is a contactlistener function I'd love to hear about it.
It was the best of times, it was the worst of times . . .
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Directional Collision Dectection

Post by MarekkPie »

You could always request that as a feature using the "Issue Tracker" button above.
Post Reply

Who is online

Users browsing this forum: No registered users and 61 guests