how to do a ray cast?

Arcanor

11-04-2007 10:25:22

I'm having trouble implementing a ray cast. Here's how I'm doing it:
bool ArcObject::isOnGround(void)
{
Ogre::Vector3 Norm;
Ogre::Vector3& rNorm = Norm;
int ColID;
int& rColID = ColID;

Ogre::Real retval = OgreNewt::CollisionTools::CollisionRayCast(mBody->getCollision(), Ogre::Vector3::ZERO, Ogre::Vector3::NEGATIVE_UNIT_Y, rNorm, rColID);

return (retval < 1);
}


When I run my code I get a memory access violation pointing to the CollisionRayCast function in ogrenewt_tools.cpp:
Ogre::Real CollisionRayCast( const OgreNewt::Collision* col, const Ogre::Vector3& startPt, const Ogre::Vector3& endPt,
Ogre::Vector3& retNorm, int& retColID )
{
return NewtonCollisionRayCast( col->getNewtonCollision(), &startPt.x, &endPt.x, &retNorm.x, &retColID );
}

Can anyone tell me what's wrong with my code, or provide a code snippet?

By the way, is there a repository of code snippets somewhere? I looked through the API documentation for both Newton and OgreNewt, the OgreNewt demo sample sources, and the StuntPlayground source as well and couldn't find any usage example.

Thanks in advance.

Arcanor

11-04-2007 11:10:36

I think my problem stems from that I'm trying to do a "CollisionRayCast" instead of a "BasicRayCast". I will change my code and see if that helps.

Arcanor

13-04-2007 03:13:14

I was able to get my BasicRaycast working. The problem was that I was using getCollision() to try to find the bottom of the AABB and use that as a starting point for my ray. However, I had been deleting the collision object as soon as the body was assigned, and this was causing memory issues when I tried to access it for my raycast, obviously. Now that I'm no longer deleting the collision I'm able to successfully do the raycast.

This leaves a question:

I had originally decided to immediately delete the collision because that was what was done in the very first ogrenewt tutorial (in the ogre website wiki). Should the collision be deleted for an important reason, i.e. will it cause a problem or memory bloating if I don't delete it?

walaber

13-04-2007 07:15:14

no problem, just remember to delete it when you don't need it anymore!