Angle of face

markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Angle of face

Post by markhula »

Hi all,

I am a newbie and need a bit of help.
I need to find out the angle of a face (or faces) when one mesh collides with another.

I presume that I need to raycast from my mesh 'through' the corresponding mesh.
Then once I have a hit 'somehow' from the faces intersected I can get an 'average' angle.
The testing mesh is a dynamic body (so has no rolling physics) and the mesh to test against is a static triangle mesh.

Basically all I want to find is the 'angle' (presumably a vector) at a point of the triangle mesh. Then by firing multiple rays over an area I can again 'somehow' :-) , the the average angle of all the faces and apply that to my player mesh.

??????

Any help appreciated.

Cheers
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Angle of face

Post by pico »

Hi,

you can cast three rays in form of a triangle and remember each hitpoint. Then you can calc the normal of this 'triangle' like this:

//hp1-hp3 are the three hitpoints from the ray cast
vector3 normal = CrossProduct(hp2 - hp1,hp3 - hp1);
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

Hey there,

Thanks for that!
Can I assume ( :oops: ) that on a collisioncallback the m_normalWorldOnB (!!) is infact the normal (angle of face I interpret that as) of the object I am colliding with?
If this is true do I need to do multiple raycasts, or is that 1 normal sufficient (i.e. the collision could be across multiple faces).

Not sure if that's clear! :lol:

Cheers - your help is appreciated
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Angle of face

Post by pico »

markhula wrote:Hey there,

Thanks for that!
Can I assume ( :oops: ) that on a collisioncallback the m_normalWorldOnB (!!) is infact the normal (angle of face I interpret that as) of the object I am colliding with?
If this is true do I need to do multiple raycasts, or is that 1 normal sufficient (i.e. the collision could be across multiple faces).

Not sure if that's clear! :lol:

Cheers - your help is appreciated
Hi,

it may be the seperating normal, not in all cases the normal of the face (not 100% sure about that).
I suggest you simply visualize the normals you get from the callback and look if it fits your needs.
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

Ermmm, :roll: , please give further detail.

What exactly is m_normalWorldonB in a collision?

How do I convert this normal to rotations or just apply the surface normal to my player mesh btTransform??? (I might be asking the wrong questions!! :lol: )

Cheers
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Angle of face

Post by pico »

markhula wrote:Ermmm, :roll: , please give further detail.

What exactly is m_normalWorldonB in a collision?

How do I convert this normal to rotations or just apply the surface normal to my player mesh btTransform??? (I might be asking the wrong questions!! :lol: )

Cheers
Think of it as a vector along both bodies are separated when in collision.
Transforming a normal to a rotation can be done with help of a quaternion. However, here it gets complicated especially when you want todo this instantly and not interpolated. You may want to google for shortestArc quaternions.
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

:oops:
I appreciate your reply, but am confused! (unsurprisingly).

If indeed m_normalWorldonB is what I want, then can't I just apply that directly to my character mesh? (which is dynamic so doesn't have rolling physics)???
Still completely lost here :roll:

Cheers
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Angle of face

Post by pico »

markhula wrote::oops:
I appreciate your reply, but am confused! (unsurprisingly).

If indeed m_normalWorldonB is what I want, then can't I just apply that directly to my character mesh? (which is dynamic so doesn't have rolling physics)???
Still completely lost here :roll:

Cheers
As i understand is that you got a normal and you want to align your character to that normal, right? If this is what you want then you need todo what i outlined in my last post. If not, please outline what you exactly wanna do :)
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

:)

Yes, that's exactly what I want to do.
I shall look into shortestarc quaternions.

Any other help though still appreciated!

Cheers
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

Hi,

I'm still struggling with this.
I am unsure how to implement it in bullet, forgive pseudo code.

As far as I understand it.

t_norm=m_normalWorldOnB //so terrain normal

pl_norm=???? //player normal but just 0,0,1 ? (player mesh is just a level cube) (so this is always the same?)

vec3 vec=crossproduct(t_norm,pl_norm);
float d= dotproduct(t_norm,pl_norm);
float s= sqrt((1+d)*2);

quat q;

q.x=vec.x/s;
q.y=vec.y/s;
q.z=vec.z/s;
q.w=s/2.0f;

Then I rotate my player mesh by applying the quaternion q?

?????? :D

Cheers
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

If I just printf m_normalWorldonB the components of it seem to vary greatly even when just on a flat plane.
Is this correct???
I assumed this particular problem had probably been asked many times in a gaming context of bullet and yet I am having difficultly finding a solution.

:cry: :cry:


Cheers
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Angle of face

Post by pico »

Given the above code is correct then you are done. You just need to apply that quaternion to your character.
As far as i remember the GPG shortest arc code can not handle when you want to go upside down because the dot product will fail in that case.

//If I just printf m_normalWorldonB the components of it seem to vary greatly even when just on a flat plane.
If the fluctuation is something small like 0.01 you don't need to worry much.

You may want to lerp the quaternions or average the normals for a smooth movement.
Finally you should implement character movement with the character controller concept.
Doing it with Bullets callbacks won't give you the behavior and quality you may want to go for (ie like in a game).
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

Hi there,

Well I implemented what * *thought* is correct and naturally it would appear to be wrong. :(
The alignment relative to the mesh is completely incorrect and the player mesh frequently 'spazzies' out with incorrect rotations all over the place (even on a flat mesh).

I really do need a more concrete example or someone to give me a greater understanding.

Also (and very importantly) that m_normalWorldonB *IS* what I want!!!, I am just assuming it's the normal of the collided face but can't find anything to prove (or disprove it).

More help required please, by anyone! :roll:

Cheers
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Angle of face

Post by pico »

markhula wrote: Also (and very importantly) that m_normalWorldonB *IS* what I want!!!, I am just assuming it's the normal of the collided face but can't find anything to prove (or disprove it).
I suggest to draw the normals you want to inspect. You can use the bullet debug drawer for that.
markhula
Posts: 34
Joined: Thu Jun 17, 2010 10:11 am

Re: Angle of face

Post by markhula »

I'm not even sure 'how' to draw a normal.
And what do I actually draw?????, if it's components are (for example) 0,0,0.1 ; what does that represent? and how do I convert that to draw coordinates?
I am obviously lacking lots of knowledge in this field and am basically just stumbling in the dark hoping to hit the light switch :?

Actually if I quat.setRotation(q) //with 'q' being the supposed quaternion shortestarc my mesh doesn't get drawn (or at least I can't see it). Is this the correct method to apply a quaternion rotation?