bool CRaycastTankExample::OnEvent(const SEvent& event) { if (!device) return false; switch(event.EventType) { case EET_MOUSE_INPUT_EVENT: { if(event.MouseInput.Event==EMIE_RMOUSE_PRESSED_DOWN) { shootSphere(vector3df(0.2,0.2,0.2), 0.2); } } break; case EET_KEY_INPUT_EVENT: { KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; if(event.KeyInput.Key == KEY_KEY_P && event.KeyInput.PressedDown == false) { world->pauseSimulation(!world->simulationPaused()); } else if(event.KeyInput.Key == KEY_KEY_R && event.KeyInput.PressedDown == false) { // Re-Spawn our tank at the original location. irr::core::matrix4 mat; mat.setTranslation(spawnPoint); tank->setWorldTransform(mat); tank->setAngularVelocity(vector3df(0,0,0)); tank->setLinearVelocity(vector3df(0,0,0)); } if(event.KeyInput.Key == KEY_SPACE && event.KeyInput.PressedDown == false) { IAnimatedMeshSceneNode *node = static_cast<IAnimatedMeshSceneNode*>(tank->getCollisionShape()->getSceneNode()); vehicle->getRigidBody()->applyImpulse(vector3df(0,0,-500), node->getJointNode("Muzzle")->getPosition(), ERBTS_LOCAL); createMuzzleFlash(node->getJointNode("Muzzle")); } } break; default: break; } return false; }
int main() { IrrlichtDevice *device = createDevice( video::EDT_OPENGL, dimension2d<u32>(800, 600)); IVideoDriver* driver = device->getVideoDriver(); ISceneManager* smgr = device->getSceneManager(); smgr->addCameraSceneNode(0, vector3df(0,10,-10), vector3df(0,5,0)); IAnimatedMesh* mesh = smgr->getMesh("ninja.b3d"); IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh); node->setMaterialFlag(EMF_LIGHTING, false); node->setMaterialTexture( 0, driver->getTexture("nskinrd.jpg") ); node->setRotation(vector3df(0,180,0)); // let ninja be in front to us node->setJointMode(EJUOR_CONTROL); // tell irrlicht that you want to control joint positions IBoneSceneNode* bone = node->getJointNode("Joint10"); // create IBoneSceneNode and select the desired bone while(device->run()) { bone->setRotation(bone->getRotation() + vector3df(1,0,0)); // rotate bone driver->beginScene(); smgr->drawAll(); driver->endScene(); } device->drop(); return 0; }
void CRaycastTankExample::createTank(const stringw file, const stringw collFile, const vector3df &pos, const f32 mass) { IAnimatedMeshSceneNode *Node = device->getSceneManager()->addAnimatedMeshSceneNode( device->getSceneManager()->getMesh(file.c_str())); Node->setPosition(pos); //Node->setRotation(vector3df(-40,90,0)); Node->setMaterialFlag(video::EMF_LIGHTING, true); //Node->setScale(vector3df(2,2,4)); IGImpactMeshShape *shape = new IGImpactMeshShape(Node, device->getSceneManager()->getMesh(collFile.c_str()), mass); tank = world->addRigidBody(shape); // When using a raycast vehicle, we don't want this rigid body to deactivate. tank->setActivationState(EAS_DISABLE_DEACTIVATION); // Set some damping on the rigid body because the raycast vehicles tend to bounce a lot without a lot of tweaking. // (cheap fix for the example only) tank->setDamping(0.4, 0.4); // We create our vehicle, passing our newly created rigid body as a parameter. vehicle = world->addRaycastVehicle(tank); // Set up our wheel construction info. These values can be changed for each wheel, // and the values that you want to keep will stay the same, that way // all parameters for each wheel can stay the same for what needs to remain equal, // such as directions and suspension rest length. SWheelInfoConstructionInfo wheel; wheel.chassisConnectionPointCS = vector3df(0.0,-0.88,4.0); wheel.wheelDirectionCS = vector3df(0.0,-0.1,0.0); wheel.wheelAxleCS = vector3df(-0.5,0.0,0.0); wheel.suspensionRestLength = 0.6; wheel.wheelRadius = 8.0; wheel.isFrontWheel = true; // The bones are in the center of the mesh on the X axis, so we just set the width ourselves. // Do the left row of wheels. for(u32 i=0; i < Node->getJointCount(); i++) { // The bones that we need in this mesh are all named "RoadWheels" with a numerical suffix. // So we do a quick check to make sure that no unwanted bones get through as wheels. if(Node->getJointNode(i)->getName()[0] == 'R') { wheel.chassisConnectionPointCS = vector3df(-4, Node->getJointNode(i)->getPosition().Y,Node->getJointNode(i)->getPosition().Z); vehicle->addWheel(wheel); } } wheel.wheelAxleCS = vector3df(0.5,0.0,0.0); // Do the right row of wheels. for(u32 i=0; i < Node->getJointCount(); i++) { if(Node->getJointNode(i)->getName()[0] == 'R') { wheel.chassisConnectionPointCS = vector3df(4, Node->getJointNode(i)->getPosition().Y,Node->getJointNode(i)->getPosition().Z); vehicle->addWheel(wheel); } } for (u32 i=0;i<vehicle->getNumWheels();i++) { SWheelInfo &info = vehicle->getWheelInfo(i); info.suspensionStiffness = 0.08f; info.wheelDampingRelaxation = 20.0f; info.wheelDampingCompression = 20.0f; info.frictionSlip = 1000; info.rollInfluence = 0.1f; // We call updateWheel, which takes SWheelInfo as the first parameter, // and the ID of the wheel to apply that info to. This must // be called after any changes in order for the changes to actually take effect. vehicle->updateWheelInfo(i); } }