Code Snippets

Annoying piece of code

This piece of code is annoying me.

// Get the time between frames
float deltaTime = Timing::getInstance()->timeBetweenFrames();

// If the time between frames is greater than our timestep, set it to the timestep
if (deltaTime>PhysicsManager::getInstance()->getTimeStep())
deltaTime = PhysicsManager::getInstance()->getTimeStep();

// Add the time between frames to the accumulator
accumulator += deltaTime;

// If the accumulator is greater than the timestep we update the physics(with the timestep value)
while(accumulator>=PhysicsManager::getInstance()->getTimeStep())
{
// We subtract the timestep from the accumulator
accumulator-=PhysicsManager::getInstance()->getTimeStep();

// Update the physics
PhysicsManager::getInstance()->updatePhysicsManager();
}

It’s supposed to implement a framerate independent physics update. Although I don’t think it’s working! It’s adapted from Fix your timestep

float t = 0.0f;
const float dt = 0.01f;

float currentTime = time();
float accumulator = 0.0f;

while (!quit)
{
float newTime = time();
float deltaTime = newTime - currentTime;
currentTime = newTime;

accumulator += deltaTime;

while (accumulator>=dt)
{
integrate(state, t, dt);
t += dt;
accumulator -= dt;
}

render(state);
}

I haven’t given it much thought yet, but hope to figure out whats going wrong soon.

If I just step the physics every frame the physics seems to run faster and so does the framerate.

Any ideas?

Liam

Tags:

Thursday, May 27th, 2010 Blog, Code Snippets View Comments

Doxygen Example

Here is a class documented for use with Doxygen:

Header File:

#ifndef PLAYER_H
#define PLAYER_H
/// Player Ship class
/** This class contains all the data the factory needs to be created and updated. */

#include "BaseApplication.h"

using namespace Ogre;

class Player
{

    private:

        int mHealth;///<Player health

        int mLastFiredTime;///<Player last fired time

        int mCurrentTime ;//<Player current fire time

        int mFireDelay;///<Player fire delay

        float mSpeed;///<Player speed

        bool mAlive;///<Used to check if alive

        Vector3 mDirection;///<Player direction vector

        Vector3 mVelocity;///<Player velocity

        Entity* mPlayerEntity;    ///<Player entity    

        SceneNode* mPlayerNode;///<Main node that we will control

        SceneNode* mShipNode;///<Ship node attached to mPlayerNode

        SceneNode* mCameraNode;///<Camera node attached to mPlayerNode

        SceneNode* mExhaustNode;///<Exhaust node attached to mPlayerNode

        ParticleSystem* pExhaustSys;///<Particle system attached to mPlayerNode

        SceneManager * mSceneMgr;///<Pointer to scene manager used to destroy objects

    public:

        ~Player();///<Destructor used when deleting a player

        void createPlayer(SceneManager *mSceneMgr);///<Creates a player

        void createPlayer(SceneManager *mSceneMgr, Camera *mCamera);///<Creates a player

        void move();///<Increases movement speed

        void rotate(float Angle);///<Rotates the player

        void update();///<Updates the player

        Vector3 getPosition(){return mPlayerNode->getPosition();}///<Gets the position of the player

        Quaternion getOrientation(){return mPlayerNode->getOrientation();}///<Gets the orientation of the player

        Vector3 getDirection();///<Gets direction the factory is facing

        bool isAlive(){return mAlive;}///<Checks if alive

        bool canFire();///<Checks if player can fire

        void receivePacketData(PacketStream & streamIn);///<Receives Packet Data

        void writePacketData(PacketStream & streamIn);///<Writes Packet Data

};
#endif

Cpp File:

#include "player.h"

///~Player()
/** This uses the scene manager to destroy the player. */
Player::~Player()
{
	mSceneMgr->destroyEntity(mPlayerEntity);
	mSceneMgr->destroySceneNode(mShipNode);
	mSceneMgr->destroySceneNode(mExhaustNode);
	mSceneMgr->destroyParticleSystem(pExhaustSys);
	mSceneMgr->destroySceneNode(mPlayerNode);
}

void Player::writePacketData(PacketStream & streamIn)
{
		float xpos = mPlayerNode->getPosition().x;
		float zpos = mPlayerNode->getPosition().z;
        streamIn.writeInt(xpos);
        streamIn.writeInt(zpos);
        streamIn.writeInt(mVelocity.x);
		streamIn.writeInt(mVelocity.z);
} 

void Player::receivePacketData(PacketStream & streamIn)
{
		float xpos,zpos;
		streamIn.readInt(xpos);
		streamIn.readInt(zpos);
		mPlayerNode->setPosition(xpos,getPosition().y,zpos);
		streamIn.readInt(mVelocity.x);
		streamIn.readInt(mVelocity.z);
} 

///createPlayer()
/** This creates a default player.
	\param SceneMgr the base application SceneManager
	\param camera the ogre camera
*/
void Player::createPlayer(SceneManager *sceneMgr)
{
	mAlive = true;
	mSpeed = 0;
	mLastFiredTime = 0;
	mCurrentTime = 0;
	mFireDelay = 500;
	mVelocity = (1,0,1);
	mSceneMgr = sceneMgr;
	mPlayerNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	mPlayerEntity = mSceneMgr->createEntity("Player2", "razor.mesh");
	mShipNode = mPlayerNode->createChildSceneNode();
	mShipNode->attachObject(mPlayerEntity);
	mShipNode->rotate(Vector3::UNIT_X,-Degree(90));
	mPlayerNode->setPosition(BaseApplication::GAMEWORLD_MAX/2,0,BaseApplication::GAMEWORLD_MAX/2);
	mExhaustNode = mPlayerNode->createChildSceneNode();
	pExhaustSys = mSceneMgr->createParticleSystem("playerExhaus2t", "Examples/JetEngine2");
	pExhaustSys->setVisible(true);
	mExhaustNode->setPosition(0,0,-6);
	mExhaustNode->attachObject(pExhaustSys);
}

///createPlayer()
/** This creates a default player.
	\param SceneMgr the base application SceneManager
	\param camera the ogre camera
*/
void Player::createPlayer(SceneManager *sceneMgr, Camera *camera)
{
	mAlive = true;
	mSpeed = 0;
	mLastFiredTime = 0;
	mCurrentTime = 0;
	mFireDelay = 500;
	mVelocity = (1,0,1);
	mSceneMgr = sceneMgr;
	mPlayerNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	mPlayerEntity = mSceneMgr->createEntity("Player", "spaceShip.mesh");
	mShipNode = mPlayerNode->createChildSceneNode();
	mShipNode->attachObject(mPlayerEntity);
	mShipNode->rotate(Vector3::UNIT_X,-Degree(90));
	mCameraNode = mPlayerNode->createChildSceneNode();
	mCameraNode->attachObject(camera );
	mCameraNode->setPosition( Vector3( 0, 450, -150) );
	mPlayerNode->setPosition(BaseApplication::GAMEWORLD_MAX/2,0,BaseApplication::GAMEWORLD_MAX/2);
	camera->lookAt(mPlayerNode->getPosition()-mCameraNode->getPosition());
	mExhaustNode = mPlayerNode->createChildSceneNode();
	pExhaustSys = mSceneMgr->createParticleSystem("playerExhaust", "Examples/JetEngine2");
	pExhaustSys->setVisible(true);
	mExhaustNode->setPosition(0,0,-6);
	mExhaustNode->attachObject(pExhaustSys);
}

///move()
/** This speeds up the player to a max speed.*/
void Player::move()
{
	if(mSpeed<4)
		mSpeed += 0.6f;
}

///rotate()
/** This rotates the player.
\param Angle by which player is rotated about the Y axis
*/
void Player::rotate(float Angle)
{
	mPlayerNode->rotate(Vector3::UNIT_Y,Degree(Angle));
}

///getDirection()
/** This gets a Vector3 representing the player direction.
*/
Vector3 Player::getDirection()
{
	Matrix3 matrix;
	mPlayerNode->getOrientation().ToRotationMatrix(matrix);
	mDirection = matrix.GetColumn(2);
	return mDirection;
}

///update()
/** This updates the player.
It wraps the player.
It moves the player.
It updates current time.
*/
void Player:: update()
{
	if(mSpeed>0.00){
		mSpeed -= 0.05f;
	}
	if(mPlayerNode->getPosition().x>BaseApplication::WORLD_MAX)
		mPlayerNode->setPosition(BaseApplication::WORLD_MIN,0,mPlayerNode->getPosition().z) ;
	else if(mPlayerNode->getPosition().x<BaseApplication::WORLD_MIN)
		mPlayerNode->setPosition(BaseApplication::WORLD_MAX,0,mPlayerNode->getPosition().z);
	if(mPlayerNode->getPosition().z>BaseApplication::WORLD_MAX)
		mPlayerNode->setPosition(mPlayerNode->getPosition().x,0,BaseApplication::WORLD_MIN) ;
	else if(mPlayerNode->getPosition().z<BaseApplication::WORLD_MIN)
		mPlayerNode->setPosition(mPlayerNode->getPosition().x,0,BaseApplication::WORLD_MAX);

	mVelocity = getDirection();
	mVelocity.x *= mSpeed;
	mVelocity.z *= mSpeed;
	mPlayerNode->setPosition(mPlayerNode->getPosition() + mVelocity);
	mCurrentTime = GetTickCount();

}

///canFire()
/** This checks mCurrentTime against mLastFiredTime. If it's greater than mFireDelay you can fire again.
*/
bool Player::canFire()
{
	if (mCurrentTime > (mLastFiredTime+mFireDelay)){
		 mLastFiredTime = mCurrentTime;
		return true;
	}
	else
		return false;
}
Monday, January 19th, 2009 Code Snippets View Comments