NewtonWorldRayCast ray hit location

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

NewtonWorldRayCast ray hit location

Postby Boost » Mon Dec 02, 2013 2:18 pm

Hey,

I've been using Newton for the past couple of months and really liking it so far,
but I ran into an issue while trying to implement an AI.
The problem is that I need the AI to try to stop a ball when it is moving towards it's goal.
After trying to find a solution I found out that I can get the object the ball is going to hit with ray casting.

The problem is that I cannot get the ray cast to give me a proper position of the contact.
I tried looking for a way to get the position of ray intersection in the callback, but
using code to get it from const dFloat* const hitContact seems to return always the same position for all rays
hitting a specific object.
Is this a correct way of doing this?
Code:
Code: Select all
   RayCastData* data = reinterpret_cast<RayCastData*>(userData);

   data->HitEntities.push_back(new RayCastHitEntity(body, Float3(*hitContact, *(hitContact+sizeof(dFloat)), *(hitContact+sizeof(dFloat)*2))));

   // Check for end //
   if(data->HitEntities.size() >= (size_t)data->MaxCount)
      return 0.f;

   // Continue //
   return 1.f;

which seems to return sane looking points, but they are always the same when hitting the same object.
If this is not what the parameter is for, it'd be great to have a parameter in the callback specifying the position of intersection.
Would be great if somebody could enlighten me on this issue, thanks.
Boost
 
Posts: 2
Joined: Mon Dec 02, 2013 2:03 pm

Re: NewtonWorldRayCast ray hit location

Postby JoeJ » Mon Dec 02, 2013 2:49 pm

Below is what i'm using.
To get the intersection point i'd need to do:

RayCastInfo info;
sVec3 p0(0,0,0); sVec3 p1(10,0,0);
RayCast (p0, p1, info);

sVec3 hitLoc = p0 + (p1-p0) * info.param;

... hope that helps.


Code: Select all

struct RayCastInfo
{
   float param;
   sVec3 normal;
   Body* hitBody;

RayCastInfo ()
   {
      param = 1.1f;
      hitBody = 0;
   }
};

void RayCast (sVec3 &p0, sVec3 &p1, RayCastInfo &rc)
   {
      NewtonWorldRayCast (world, &p0[0], &p1[0], (NewtonWorldRayFilterCallback)nRayCastFilter, (void*)&rc, nRayCastPrefilter, 0);
   }

float nRayCastFilter (const NewtonBody* const body, const NewtonCollision* const shapeHit, const float* const hitContact, const float* const normal, long long collisionID,  void* const userData, float intersectParam)
{
   RayCastInfo *rc = (RayCastInfo*) userData;
   if (intersectParam < rc->param)
   {
      rc->param = intersectParam;
      rc->hitBody = (Body*) body;
      sMat4 m; BodyGetMatrix ((Body*)body, m);
      rc->normal = m.Unrotate (*((sVec3*)normal));
   }
   return intersectParam;
}
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonWorldRayCast ray hit location

Postby Boost » Tue Dec 03, 2013 1:43 pm

That seems to return right positions, thanks.
Now to fix my AI.
Boost
 
Posts: 2
Joined: Mon Dec 02, 2013 2:03 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests