Ninja Walking Character

From Ogre Wiki

Jump to: navigation, search

This is OgreOde SimpleScene 8, OgreOde Walking Character

How to use it:

  • open your Demo_SimpleScenes and add new header file: SimpleScenes_Ninja.h

save it in demos\SimpleScenes\include

#ifndef _SIMPLESCENES_NINJA_H_
#define _SIMPLESCENES_NINJA_H_

class SimpleScenes_Ninja:public SimpleScenes
{
 public:
  SimpleScenes_Ninja(OgreOde::World *world) : SimpleScenes(world) 
  {
   Entity* hero_ent = _mgr->createEntity("ninja","ninja.mesh");
   SceneNode* hero_node = _mgr->getRootSceneNode()->createChildSceneNode("ninja");
   hero_node->attachObject(hero_ent);
   hero_node->scale(0.01,0.01,0.01);

   AxisAlignedBox aab = hero_node->getAttachedObject("ninja")->getBoundingBox(); 
   Ogre::Vector3 min = aab.getMinimum()*hero_node->getScale();
   Ogre::Vector3 max = aab.getMaximum()*hero_node->getScale();
   Ogre::Vector3 center = aab.getCenter()*hero_node->getScale();
   Ogre::Vector3 size(fabs(max.x-min.x),fabs(max.y-min.y),fabs(max.z-min.z));
   float radius = (size.x>size.z)?size.z/2.0f:size.x/2.0f;
 
   OgreOde::Body* dollFeetBody = new OgreOde::Body(_world,"feet");
   dollFeetBody->setMass(OgreOde::SphereMass(70*2.5,radius));
   OgreOde::SphereGeometry* feetGeom = new OgreOde::SphereGeometry(radius,_world);
   OgreOde::TransformGeometry* feetTrans = new OgreOde::TransformGeometry(_world,_space); 


   feetTrans->setBody(dollFeetBody); 
   feetTrans->setEncapsulatedGeometry(feetGeom);
   hero_node->attachObject(dollFeetBody);
   _feet = dollFeetBody;
   _bodies.push_back(_feet);
   _geoms.push_back(_feet->getGeometry(0));

   OgreOde::Body* dollTorsoBody = new OgreOde::Body(_world,"torso"); 
   dollTorsoBody->setMass(OgreOde::CapsuleMass(70*2.5,radius,Vector3::UNIT_Y,radius)); 
   dollTorsoBody->setAffectedByGravity(false);
   OgreOde::TransformGeometry* torsoTrans = new OgreOde::TransformGeometry(_world,_space); 
   OgreOde::CapsuleGeometry* torsoGeom = new OgreOde::CapsuleGeometry(radius,size.y-4*radius,_world); 
   torsoGeom->setPosition(Ogre::Vector3(0,size.y-((size.y-4*radius)/2+2*radius),0)); 
   torsoGeom->setOrientation(Quaternion(Degree(90),Vector3::UNIT_X));
   torsoTrans->setBody(dollTorsoBody); 
   torsoTrans->setEncapsulatedGeometry(torsoGeom);
   hero_node->attachObject(dollTorsoBody);
   _torso = dollTorsoBody;
   _bodies.push_back(_torso);
   _geoms.push_back(_torso->getGeometry(0));

   OgreOde::HingeJoint* joint = new OgreOde::HingeJoint(_world);
   joint->attach(dollTorsoBody,dollFeetBody);
   joint->setAxis(Ogre::Vector3::UNIT_X);
   _joints.push_back(joint);	
  }

  virtual ~SimpleScenes_Ninja(){}

  virtual void frameEnded(Real time, OIS::Keyboard* input, OIS::Mouse* mouse)
  {
   SimpleScenes::frameEnded(time, input, mouse);

   _ninja_rotate = 0;
   if (input->isKeyDown(KC_J))
    _ninja_rotate += -1;
   if (input->isKeyDown(KC_L))
    _ninja_rotate += 1;

   _ninja_thrust = 0;
   if (input->isKeyDown(KC_I))
    _ninja_thrust += -1;
   if (input->isKeyDown(KC_K))
    _ninja_thrust += 1; 

   if (_ninja_rotate == 0)
   {
    _feet->wake();
    _feet->setAngularVelocity(Vector3(_feet->getAngularVelocity().x,0,0));
   }
   else
   {
    _feet->wake();
    Quaternion q1 = _torso->getOrientation();
    Quaternion q2(Degree(-4*_ninja_rotate),Ogre::Vector3::UNIT_Y);
    _torso->setOrientation(q1*q2);
   }

   if (_ninja_thrust == 0)
   {
    _feet->wake();
    _feet->setLinearVelocity(Vector3(0,_feet->getLinearVelocity().y,0));
    _feet->setAngularVelocity(Vector3(0,_feet->getAngularVelocity().y,0));
   }
   else
   {
    float speed = 30;
    _feet->wake();
    Quaternion q = _torso->getOrientation();
    _feet->setAngularVelocity(q*Ogre::Vector3(speed*_ninja_thrust*cos(1.0),_feet->getAngularVelocity().y,0));
   }

   Quaternion q = _torso->getOrientation();         
   Vector3 x = q.xAxis();
   Vector3 y = q.yAxis();
   Vector3 z = q.zAxis();
   _torso->wake();
   _torso->setOrientation(Quaternion(x,Vector3::UNIT_Y,z));
   _torso->setAngularVelocity(Vector3(0,0,0)); 

   OgreOde::Body* body = 0;
   if (input->isKeyDown(KC_Z)) 
    body = createRandomObject(OgreOde::Geometry::Class_Sphere);
   else if (input->isKeyDown(KC_X)) 
    body = createRandomObject(OgreOde::Geometry::Class_Box);
   else if (input->isKeyDown(KC_C)) 
    body = createRandomObject(OgreOde::Geometry::Class_Capsule);
   else if (input->isKeyDown(KC_T)) 
    body = createRandomObject(OgreOde::Geometry::Class_TriangleMesh);
  }

 virtual const String& getName()
 {
  static String name = "Ninja Walking Character";
  return name;
 }

 virtual const String& getKeys()
 {
  static String keys = "IJKL - move character Z-sphere X-box C-capsule T-trimesh";
  return keys;
 }

 protected:
  OgreOde::Body *_hero;
  OgreOde::Body *_feet;
  OgreOde::Body *_torso;
  int _ninja_rotate,_ninja_thrust;
};

#endif
  • open SimpleScenesApplication.cpp and add
#include "SimpleScenes_Ninja.h"
  • in the same file find this:
else if (myInput->isKeyDown(KC_F7))
{
 delete _test;
 _test = new SimpleScenes_Zombie(_world);

 changed = true;
}
  • and change to this:
else if (myInput->isKeyDown(KC_F7))
{
 delete _test;
 _test = new SimpleScenes_Zombie(_world);

 changed = true;
}
else if (myInput->isKeyDown(KC_F8))
{
 delete _test;
 _test = new SimpleScenes_Ninja(_world);

 changed = true;
}
  • compile&run
  • press f8 to run ninja simple scene


This is just code of OgreOde Walking Character rewriten to SimpleScene and last Ogre&OgreOde

Write by Radsun

Thx to rewb0rn

Personal tools
administration