Help with raycast

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Help with raycast

Postby KingSnail » Thu Feb 04, 2010 3:35 pm

Can someone please share some code of your ray cast function if you are using directx,
I have been stuck on this problem for months, tried a million different ways of making rays, nothing works
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with raycast

Postby Julio Jerez » Thu Feb 04, 2010 6:01 pm

If you post the function you wrote to inteface with the Raycast, I may be able to help you out?

Basically it is a matter of setting a call back and douns soem trival vetor scaling.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with raycast

Postby Stucuk » Thu Feb 04, 2010 6:04 pm

It shouldn't make any difference if you are using Direct X or OpenGL. All you basically do is give it 2 3D coordinates. As long as you use only one type of coordinate system for the Rays as well as the actual collisions themself, it should work fine. You should be able to just copy and paste the raycast part of a newton tutorial and modify it.

The following is an example of the code im using:

Prefilter/Filter Callbacks
Code: Select all
function RayPrefilterCallback(const body : PNewtonBody; const collision : PNewtonCollision; userData : Pointer) : cardinal; cdecl;
begin
 Result := 1;
end;

var
PickedParam : Float;
PickedBody  : PNewtonBody;

function RayFilterCallback( const body : PNewtonBody; const hitNormal: PFloat; collisionID : Int; userData: Pointer; intersetParam: Float ) : Float; cdecl;
begin
 if intersetParam < PickedParam then
 begin
  PickedParam := intersetParam;
  PickedBody  := body;
 end;
 Result := intersetParam;
end;

RayCast
Code: Select all
PickedBody  := Nil;
PickedParam := 1.1;
NewtonWorldRayCast(World,Point1,Point2,@RayFilterCallback,Nil,@RayPrefilterCallback);
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Help with raycast

Postby KingSnail » Thu Feb 04, 2010 7:10 pm

I tried using the tutorial it uses gluunproject, I tried the directx version of it D3DXVec3Unproject, I dont really understand it, it needs a world matrix of something? The way I understand it is I have a world matrix for every object in the world.
So does that mean I need to loop through all my objects? confused :S

I also tried just using simple vectors to plug into the newton raycasting function

D3DXVECTOR3 p0(10.0f, 5.0f, 0.0f);
D3DXVECTOR3 p1(10.0f, 5.0f, 1000.0f);

Those are both in global space ? I think, I draw them like this in the world just by making a line using those 2 vertices.

It clearly intersects a body:

Image

But it doesnt detect that its a mass 1 object.
The way I filter it is that the level walls have mass 0 and all objects are above 0, here is my filter

Code: Select all
static unsigned RayCastPrefilter (const NewtonBody* body,  const NewtonCollision* collision, void* userData)
{
   //if(NewtonCollisionIsTriggerVolume(collision))return 0;

   dFloat mass;
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);

   extern float Display;
   Display = mass; //Prints to the screen, the mass, so if it says 1 then theres a collision with a chair, if 0 then its level

   return (mass > 0.0f) ? 1 : 0;
}


I also tried using the code from http://www.toymaker.info/Games/html/picking.html, Doesnt seem to give me any results either.

Sometimes I do see a 1 and I get happy but its not where my ray is drawn.
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with raycast

Postby Julio Jerez » Thu Feb 04, 2010 9:02 pm

I think we are getting confused here.

You said that you have a problem with ray casting, but by your description it seemes you are having problems
tryin to pick an object from the screen by shooting a ray from the cursor to the back of the frame buffer.

Basically your problem is that you do not know how to transform the points from screen space to world space. Is this correct?
we need to clarify the problem so that the solution is effective.

ther are two problems. Picking and ray casting.
I can help you with both. But first I need to kknow if the ray casting is wroking at all.
if you place a box in the world in hard code position (say teh origin) and you racy cast from pointe you know will interset the Box does the ray Works?
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with raycast

Postby Stucuk » Fri Feb 05, 2010 12:47 am

Your RayCastPrefilter is flawd. The prefilter will be sent everything that it collides with, so if your line goes through both a wall and a chair then the display float may say 0 even if it passes through a chair (Assuming the Wall is found last). Your code should be more like the following, with Display set to 0 before the NewtonWorldRaycast is called.

Code: Select all
if (mass > 0.0f) Display = mass; //Prints to the screen, the mass, so if it says 1 then theres a collision with a chair, if 0 then its level
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Help with raycast

Postby KingSnail » Sun Feb 07, 2010 8:32 pm

Thanks for replies, Stucuk you were right, it was flawed, I corrected it how you said, now what seems to happen is that
simple collision shapes like balls and cones trigger using those 2 points I said in earlier post, but the chair still does not trigger,
the chair is a compound collision. Weird :shock:

Julio: Thanks for help also, I did what you said after I fixed my filter and everything works except... for the damn compound collision, I cant figure out why because everything works as it should.
So I need to somehow fix that and also cast the ray from center of the screen outward, not just at origin

-Edit: also the chair as a convex hull collision works, just not compound collision chair
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with raycast

Postby Julio Jerez » Sun Feb 07, 2010 9:41 pm

Good to here you are getting some result,
Ray cast should work with all all collision shapes, even compound.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with raycast

Postby KingSnail » Mon Feb 08, 2010 8:16 pm

I cant get the compound collision to be detected by ray, Any tips on what to do?
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with raycast

Postby Stucuk » Mon Feb 08, 2010 9:38 pm

Have you implemented a debug display that can draw the collision shapes on the screen so you can be sure that everything is setup right?
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Help with raycast

Postby Julio Jerez » Mon Feb 08, 2010 11:32 pm

debug display is your best friend when thongs do not look liek the should.

My guess is tha teh shape are not where you think they are.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Help with raycast

Postby KingSnail » Wed Feb 10, 2010 3:55 pm

I have debug shapes, everything is perfect. and performs as it should, just cant think of what to try anymore
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with raycast

Postby JernejL » Wed Feb 10, 2010 6:15 pm

There were some versions of 2.x releases where raycasting did not work properly in some situations, make sure you have the latest one.

If this is a bug i suggest you make a test case demo for Julio.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Help with raycast

Postby Julio Jerez » Wed Feb 10, 2010 11:21 pm

yes that is right, makw sure you hav eteh later version.

and if you can send me a test demo I can debug it for you.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 397 guests

cron