gun bobbing class

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
arnir
Competition winner
Posts: 154
Joined: Sat Jan 20, 2007 4:36 pm
Location: Czech Republic

gun bobbing class

Post by arnir »

Code: Select all

/*-----------------------------------------------------------------------------*
| weaponMotion.h                                                               |
|                                                                              |
| last edit date: (5.5. 2009)                                                  |
|                                                                              |
| author:  Martin Cmar                                                         |
|                                                                              |
*-----------------------------------------------------------------------------*/

#ifndef WEAPON_MOTION_H
#define WEAPON_MOTION_H


#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;



class weaponMotion
{
ICameraSceneNode * camera;
vector3df lastPos;

public:



weaponMotion(ICameraSceneNode * cam)
{
camera = cam;
lastPos = camera->getPosition();
}


void weaponMotion::update(int time,IAnimatedMeshSceneNode * node, vector3df pos)
{

vector3df diff = camera->getPosition() - lastPos;
float scale = sqrt( diff.X * diff.X	+ diff.Z * diff.Z); 

if(scale > 8)
scale = 8;

float fracsin = sin(2 * time * 0.005);
float fracsin2 = sin( time * 0.005);
	
pos.Y += scale * fracsin * 0.1;
pos.X += scale * fracsin2 * 0.1;

node->setPosition(pos);	
lastPos = camera->getPosition();

}

};

#endif
example:

Code: Select all

weaponMotion * m = new weaponMotion(camera);
and in main loop:

Code: Select all

m->update(device->getTimer()->getTime(), mygun, defaultgunpos);
by changing numeral vaulues you edit speed, size and frequency of gun bobbing
programmer is bad designer
designer is bad programmer
psychophoniac
Posts: 101
Joined: Wed Dec 03, 2008 5:33 pm
Location: ger

Post by psychophoniac »

maybe writing this as an ISceneNodeAnimator would make sense, then you don't have to update it by hand in your main loop...!
i love skateboarding!
DarkRage4
Posts: 31
Joined: Wed Jul 29, 2009 11:07 pm
Location: Ontario, Canada
Contact:

Post by DarkRage4 »

awesome share it works for me :D
Post Reply