[0.9] RayCaster doesn't work at all.

WolverinePL

20-08-2007 13:57:49

Hi, first of all I'd like to thank authors for PhysX wrapper for Ogre, it saved me a lot of time to implement physics in my game. Unfortunately there is no way to write an application without any problems :)

I want to use this code to trace a ray in nx scene:

NxOgre::RayCaster rc(
mCharacter->mCharacter->getNode()->getPosition(),
Application::getSingleton().mCamera->getDirection().normalisedCopy(),
8000,
NxOgre::RayCaster::RCT_CLOSEST,
Application::getSingleton().mNxScene );

if( rc.castShape( NxOgre::RayCaster::ActorFilter::AF_NONE ) )
{
LogManager::getSingleton().logMessage( "castShape returned true" );
NxOgre::Actor * a = rc.getClosestActor();

if( a )
{
LogManager::getSingleton().logMessage( "Actor hit!" );
a->addForce( Application::getSingleton().mCamera->getDirection() * 10 );
}
}


and the result is that it does nothing (the castShape function is always throwing false - can't see any of this messages on the log, and yes, I'm sure that code is being executed :)). Ofcourse I have some bodies on the scene - a big trimesh representing a terrain and some boxes with the camera facing them.

mCharacter is object of my class representing player with NxOgre::Character as mCharacter in it.
Application is a singleton with such things like ogre's scene mgr, nx scene, world etc. I don't use framelistener (instead of that I use simulate and render method - other physics works fine with it).

Hope my post is understandable :)

betajaen

20-08-2007 14:41:43

Not bad for a first post. You've presented everything I need, and even normalised the directional vector which is a common oversight with newbeis.

Before we move on can you try an AnyCast. It's just to check if NxOgre/PhysX is at fault:

NxOgre::RayCaster rc(
mCharacter->mCharacter->getNode()->getPosition(),
Application::getSingleton().mCamera->getDirection().normalisedCopy(),
8000,
NxOgre::RayCaster::RCT_ANY,
Application::getSingleton().mNxScene );


In theory it should return a true/false, but no Actor.

WolverinePL

20-08-2007 14:58:37

Thanks for the answer. With RTC_ANY I've got always true, even when facing the sky, so my guess is that this is caused by incorrect origin and/or direction. Am I right?

betajaen

20-08-2007 15:19:10

It seems to be.

WolverinePL

20-08-2007 15:40:57

OK then, now I know what I'm standing on, thanks for help :)

WolverinePL

21-08-2007 18:33:01

Hi again, my problem was caused by position of the camera - it was inside a Character. When raycaster hit a Character, the casting function (RayCaster::_castClosest) simply returns with false value even if we have some other objects after Character. This turns Character into some kind of shield for raycaster and I suppose it should be reported as a bug :)

if (hitShape->getActor().userData)
mHit.mActor = (static_cast<NxActorUserData*>(hitShape->getActor().userData))->toActor();
else
return false;


I assume that Character's actor has null in userData.

betajaen

21-08-2007 18:40:55

It does. Well in future revisions it will point to the Character.

WolverinePL

22-08-2007 13:17:15

I've made a patch for this issue. Now RayCaster returns Character collisions as well.

http://pastebin.com/m64842948

It can be used like this:

NxOgre::RayCaster rc(
pos,
dir,
8000,
NxOgre::RayCaster::RCT_CLOSEST,
SomeNxScene );

if( rc.castShape( NxOgre::RayCaster::AF_NONE ) )
{
LogManager::getSingleton().logMessage( "castShape returned true" );
NxOgre::Actor * a = rc.getClosestActor();
NxOgre::Character * c = rc.getClosestCharacter();

if( a )
{
LogManager::getSingleton().logMessage( "Actor hit!" );
a->setLinearVelocity( dir * 100 );
}

if( c )
{
LogManager::getSingleton().logMessage( "Character hit!" );
}
}


I hope it'll be useful for someone :)

betajaen

22-08-2007 13:34:02

Brilliant!

I was going to write that myself, infact you stole my idea and wrote the UserData character binding. ;)

I shall commit it to NxOgre; post haste.

JoshPendergrass

29-08-2007 11:15:44

Thanks a ton for adding this :).
Is it working in 34?
I made several actors and characters some in the same spot, but can only get actors to be returned by the ray caster
using the following code:

NxOgre::RayCaster rc(
m_pOgre->m_pCamera->getRealPosition(), m_pOgre->m_pCamera->getRealDirection().normalisedCopy(),
5000, NxOgre::RayCaster::RCT_CLOSEST, m_pOgre->m_pNxScene );

if( rc.castShape( NxOgre::RayCaster::AF_NONE ) )
{
NxOgre::Actor * a = rc.getClosestActor();
NxOgre::Character * c = rc.getClosestCharacter();