# getting accurate normals/tangents with p2

## Recommended Posts

Hi!

I'm trying to orient my player (a circular p2 body) with its feet toward the scenery (convex shapes) when they touch. P2 gives me contact points on the player circle, which would be great if it were ALL contact points – but it's apparently optimized to only return one contact point per convex shape (which is enough for a collision to be ascertained). But, when the circle collides with a corner, that isn't enough to compute a sensible orientation from. The problem doesn't only occur where there are "free" corners, but also, annoyingly, where scenery polygons are split into convex shapes. And so my player will randomly, in the middle of the floor, appear to start running uphill

I've tried a lot of things already:

- Adding up the contact point vectors and computing an average: Better, but still noticably wonky

- Taking the last found contact point and pointing my feet there. this is usually a spot on the floor, but not reliably so

- Scouring the friction equations from the presolve event for tangents and averaging those – very difficult to tell what's happening, but not what I want

At this point, I really don't know how to proceed. Short of writing my own physics system that will output all contact points and not just the first, I'm out of ideas.

##### Share on other sites

I've now also tried setting up sensor shapes but the optimized p2 engine will of course return a collision as soon as it notices that one of the points that make up the sensors is inside the colliding ground object and then return that point as contactpoint.

Please, does anyone know how to do this?

##### Share on other sites

How about creating and storing the "proper" normals yourself, one per tile convex? Catch the contact event, get the corresponding proper normal, and use it.

You could compute these normals by subtracting two consecutive vertex points, rotating 90 degrees and normalizing. See how it's done in the source for p2.Narrowphase.

##### Share on other sites

That's a great idea, but with my current data I need one normal per face and not one per convex, I think - unless I can control the tesselation of my ground shapes somehow (the hard way being, of course, making them up of only quads when I create the level).

My current approach (#7 or so in total ) was to leave the sensors in place, find the shape(s) they collide with in presolve and use line.intersects on the sensor lines and all faces of the contact shape, then use the one that's closest to my player as my contact point for that shape. But I'm a bit afraid of how much cpu that's gonna take, so I'm going to take a stab at making a simple level out of quads, storing the normals with the shapes when I generate the collision objects and then using that data to orient my player.

This is way harder than I thought it would be

##### Share on other sites

Indeed, harder than I thought too ☺

Another approach that might work: snap to the closest face normal.

##### Share on other sites

Ha! It works! I could kiss you

Storing the normals in the shapes was the ticket. I added tangents too, just for kicks.