raycast & transform

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

raycast & transform

Postby blackbird_dream » Wed Mar 07, 2018 6:46 am

I need to raycast according to a relative direction. Thus I'm using this code
Code: Select all
if (m_world.Raycast(body.Position, transform.TransformDirection(LocalRef_Ray), rayLength, out hitInfo, mask))

I'm wondering it's not really correct, isn't it ? As the function transform.TransformDirection refers to the gameobject and Body.Position to the NewtonBody object which both are not synchronized.
How can I solve it ?
Thks.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: raycast & transform

Postby Sweenie » Wed Mar 07, 2018 9:48 am

Using body.position and body.rotation you could construct a Matrix4x4.
Not sure if Matrix4x4 takes a position and quaternion as constructor params but there is a function called SetTRS that can be used(passing Vector3.one as the scale component)
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: raycast & transform

Postby blackbird_dream » Wed Mar 07, 2018 10:35 am

Thks Sweenie.
I wrote this :
Code: Select all
   
Matrix4x4 matrix = Matrix4x4.identity;
Matrix4x4 invmatrix;
Vector4 rayon_ref_local;
rayon_ref_local.x = localdirX;
rayon_ref_local.y = localdirY;
rayon_ref_local.z = localdirZ;
rayon_ref_local.w = 0;
matrix.SetTRS(body.Position, body.Rotation, Vector3.one);
invmatrix = matrix.inverse;
if (m_world.Raycast(body.Position, invmatrix*rayon_ref_local, rayLength, out hitInfo, mask))

It seems to work (No error, no weird behavior)
Can you confirm it's correct please ?
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: raycast & transform

Postby Sweenie » Wed Mar 07, 2018 11:24 am

Hmm, are you sure you need to invert the matrix?
After the SetTRS call it should be enough calling matrix.MultiplyVector passing your ray vector in local space and it should return the ray vector in world space.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: raycast & transform

Postby blackbird_dream » Thu Mar 08, 2018 4:53 am

That was my cause for concern and I was not sure. You are right . Thks
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 18 guests