A simple Raycast between t2dSceneObject(s) in TGB 1.7.2
by James Ford · 04/03/2008 (10:30 am) · 3 comments
Its a bit confusing mucking around in t2dPhysics, but since I figured it out I thought I would pass it on.
You actually have all the info inside the t2dPhysics::cCollisionStatus structure but I am only returning distance because thats all I needed ... so return/use whatever you need.
// helpers.h...
// helpers.cpp:
You actually have all the info inside the t2dPhysics::cCollisionStatus structure but I am only returning distance because thats all I needed ... so return/use whatever you need.
// helpers.h...
#ifndef _HELPERS_H_
#define _HELPERS_H_
#ifndef _T2DSCENEOBJECT_H_
#include "T2D/t2dSceneObject.h"
#endif
struct CastRayInfo
{
t2dVector point;
t2dVector normal;
F32 distance;
};
#endif // _HELPERS_H_// helpers.cpp:
#include "console/consoleTypes.h"
#include "helpers.h"
bool castray( t2dSceneObject *srcObj, t2dSceneObject *dstObj, CastRayInfo& info )
{
t2dPhysics srcPhy = srcObj->getParentPhysics();
t2dPhysics dstPhy = dstObj->getParentPhysics();
t2dVector oldvel( srcPhy.getNetLinearVelocity() );
t2dVector testvel( dstObj->getPosition() - srcObj->getPosition() );
srcPhy.setGrossLinearVelocity( testvel );
t2dPhysics::cCollisionStatus status;
status.mSrcObject = srcObj;
status.mDstObject = dstObj;
status.mpSrcPhysics = &srcPhy;
status.mpDstPhysics = &dstPhy;
bool result = t2dPhysics::calculateCollision( 1.0f, &status );
if ( result )
{
info.distance = testvel.len() * status.mCollisionTimeReal;
info.point = status.mSrcContacts[0];
info.normal = status.mCollisionNormal;
}
srcPhy.setGrossLinearVelocity( oldvel );
return result;
}
ConsoleFunction( castray, F32, 3, 3, "castray( sceneObject srcObj, sceneObject dstObj )" )
{
t2dSceneObject *srcObj = NULL;
t2dSceneObject *dstObj = NULL;
if ( !Sim::findObject( argv[1], srcObj );
return NULL;
if ( !Sim::findObject( argv[2], dstObj ) )
return NULL;
CastRayInfo info;
if ( !castray( srcObj, dstObj, info ) )
return NULL;
return info.distance;
}About the author
http://jamesdev.info

Associate James Ford
Sickhead Games
castCollision is ideal for seeing what you would hit first given a velocity and time.
However, if you want to test the distance between two specific objects, "castray" should do what you want with less overhead.