[c++] ODE raycast from irr scenenode

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
xhrit
Posts: 140
Joined: Mon Jun 14, 2004 8:54 am
Location: earth
Contact:

[c++] ODE raycast from irr scenenode

Post by xhrit »

Here is code to use ODE to ray cast from an irrlicht scene node object. Useful for shooting and as a turn sensor. I assume you alredy have the the Irrlicht ODE tutorial working. scenenode is the node that you want to cast a ray from, Geom is the ODE geometry attached to the scenenode, Body is the ODE body attached to the scene node, and space is the collision space.

Code: Select all


dGeomID ray = dCreateRay(Space,1000);
bPos = scenenode->getAbsolutePosition();
const dReal *r;
r = dBodyGetRotation(Body);
dGeomSetRotation (ray,r);
dGeomSetPosition (ray,bPos.X, bPos.Y, bPos.Z);

dContactGeom contact;
dGeomRaySetLength (ray,1000);
int num = dSpaceGetNumGeoms(Space);
for (int ii=0; ii<num; ++ii) {
	dGeomID geo = dSpaceGetGeom(Space,ii);
	if ( dGeomGetClass( geo ) == dSphereClass || dGeomGetClass( geo ) == dBoxClass ){
		if (geo != Geom) {
			if ( dCollide( ray, geo, 1, &contact, sizeof(contact) ) ){
				cout << "contact with object at co-ord : " << endl;
				const dReal *tPos  = dGeomGetPosition (geo);
				cout << "x" << tPos[0] << "; y" << tPos[1] << "; z" << tPos[2] << endl;
			}
		}
	}
}
dSpaceRemove( Space, ray );
dGeomDestroy( ray );

Core2Duo E8400 3.0ghz - 2048mb DDR2 800 - geForce 9600 - Slackware12.1

Word-image-symbol programming limits, controls, and imprisons the individual.
Smash the control images, smash the control machine.
Post Reply