physics2.cpp

Go to the documentation of this file.
00001 /*
00002  * physics2.cpp
00003  *
00004  *  Created on: Jan 5, 2012
00005  *      Author: sushil
00006  */
00007 
00008 #include <assert.h>
00009 //#include <stdio.h>
00010 
00011 #include <OgreVector3.h>
00012 #include <OgreQuaternion.h>
00013 
00014 #include <physics.h>
00015 #include <ent.h>
00016 #include <utils.h>
00017 #include "DEBUG.h"
00018 
00019 void FastEcslent::Physics2D2::init(){
00020         entity->desiredSpeed = entity->speed;//kInvalidFloat;
00021         entity->desiredHeading = entity->heading;//kInvalidFloat;
00022 
00023 }
00024 
00025 /*void FastEcslent::Physics2D2::printVector(const Ogre::Vector3 &v){
00026         std::cout << "[" << v.x << ", "  << v.y << ", " << v.z << "]" << std::endl;
00027 }*/
00028 
00029 void FastEcslent::Physics2D2::doHelmsman(double dtime){// You have a desired speed and heading, get me new position and orientation
00030 
00031         if (kInvalidFloat == entity->desiredSpeed || kInvalidFloat == entity->desiredHeading)
00032                 return;
00033 
00034         DEBUG(std::cout << "Speed: " << entity->vel.length() << ", Heading: " << -atan2(entity->vel.z, entity->vel.x ) << std::endl;)
00035         //FastEcslent::Physics2D2::printVector(entity->vel);
00036 //      Ogre::Vector3 desiredVelocity = Ogre::Vector3(cos(-entity->desiredHeading) * entity->desiredSpeed, 0.0f, sin(-entity->desiredHeading) * entity->desiredSpeed);
00037 
00038 
00039         if (entity->speed < entity->desiredSpeed) {
00040                 entity->speed += (entity->maxAcceleration * dtime);
00041         } else if (entity->speed > entity->desiredSpeed) {
00042                 entity->speed -= (entity->maxAcceleration * dtime);
00043         }
00044 
00045         Ogre::Quaternion desiredRot(Ogre::Radian(entity->desiredHeading), Ogre::Vector3::UNIT_Y);
00046 
00047         //update it all
00048         entity->rot = Ogre::Quaternion::Slerp(entity->maxRotationalSpeed * dtime, entity->rot, desiredRot, true);
00049         entity->heading = entity->rot.getYaw().valueRadians();
00050         entity->yaw     = entity->heading;
00051 
00052         entity->speed = clamp(entity->speed, entity->minSpeed, entity->maxSpeed);
00053 
00054         entity->vel = Ogre::Vector3(cos(-entity->heading) * entity->speed, 0.0f, sin(-entity->heading) * entity->speed);
00055         //finally update pos
00056         entity->pos = entity->pos + (entity->vel * dtime);
00057 
00058         DEBUG(std::cout << "Speed: " << entity->speed << ", Heading: " << entity->heading << std::endl <<std::endl;)
00059 
00060 }
00061 
00062 
00063 void FastEcslent::Physics2D2::tick(double dtime){
00064         doHelmsman(dtime);
00065 }

Generated on Fri Dec 13 14:54:18 2013 for FastECSLent by  doxygen 1.5.4