(C++) parabola animator

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
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

(C++) parabola animator

Post by xDan »

I'm not actually sure if it is a parabola :P But this is some simple code used to make "gib" meshes fly off from an explosion, and then disappear when they hit a ground plane.

ParabolaAnimator.cpp

Code: Select all

#include "ParabolaAnimator.h"

ParabolaAnimator::ParabolaAnimator(
    scene::ISceneManager *smgr,
    u32 timeMs,
    const core::vector3df &start,
    const core::vector3df &vel,
    const core::vector3df &acc,
    const core::vector3df &rvel,
    const core::plane3df &groundPlane)
{
    this->smgr = smgr;
    this->lastTime = timeMs;
    this->pos = start;
    this->vel = vel;
    this->acc = acc;
    this->rvel = rvel;
    this->ground = groundPlane;
}

void ParabolaAnimator::animateNode(ISceneNode *node, u32 timeMs)
{
	if (!node) return;
	
	f32 dt = ((f32)(timeMs-lastTime)) / 1000.0;
	lastTime = timeMs;
	
	pos += vel*dt;
	vel += acc*dt;
	
	node->setPosition(pos);
	
	node->setRotation(node->getRotation() + rvel*dt);
	
	if (ground.classifyPointRelation(pos) == core::ISREL3D_FRONT)
	{
        smgr->addToDeletionQueue(node);
    }
}

ParabolaAnimator.h

Code: Select all

#ifndef __ParabolaAnimator_h
#define __ParabolaAnimator_h

#include "ISceneNode.h"

using namespace irr;
using namespace scene;

class ParabolaAnimator : public ISceneNodeAnimator
{
public:
    ParabolaAnimator(
        scene::ISceneManager *,
        u32,
        const core::vector3df &,
        const core::vector3df &,
        const core::vector3df &,
        const core::vector3df &,
        const core::plane3df &
        );
    virtual void animateNode(ISceneNode *, u32);
private:
    scene::ISceneManager *smgr;
    u32 lastTime;
    core::vector3df pos;
    core::vector3df vel;
    core::vector3df acc;
    core::vector3df rvel;
    core::plane3df ground;
};

#endif
Example usage

Code: Select all

    for (int i = 0; i < gibMeshCount; i ++)
    {
        scene::IAnimatedMeshSceneNode *n = smgr->addAnimatedMeshSceneNode(gibMeshes[i]);
        
        core::vector3df rvel;
        rvel.X = rand()%100 * ((rand()%2) ? -1 : 1);
        rvel.Y = rand()%100 * ((rand()%2) ? -1 : 1);
        rvel.Z = rand()%100 * ((rand()%2) ? -1 : 1);
        
        scene::ISceneNodeAnimator *an = new ParabolaAnimator(
            smgr,
            device->getTimer()->getTime(),
            gibPos, // initial position
            core::vector3df(rand()%200-100,100.0,rand()%200-100) / 50.0, // initial velocity
            core::vector3df(0,-5.0,0), // acceleration
            rvel, // angular velocity
            core::plane3df(0,-1.0,0, 0,1,0) // ground plane
            );
        n->addAnimator(an);
        an->drop();
    }
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

you should make a fallback device to destroy after time also... incase a "gib" falls off the map.

a parabola is described as perpindicular rounded conical "dimple" in a plane.

Code: Select all

.                .
 .              .
  .           .
    .       .
       .  .
olivehehe_03
Posts: 157
Joined: Tue Mar 20, 2007 8:30 am

Post by olivehehe_03 »

Midnight wrote:you should make a fallback device to destroy after time also... incase a "gib" falls off the map.

a parabola is described as perpindicular rounded conical "dimple" in a plane.

Code: Select all

.                .
 .              .
  .           .
    .       .
       .  .
...and as far as the maths behind it goes, y = x squared, or - x squared for an upside down one.

Anyways, this seems pretty useful, especially for having gibs coming off and explosion :D
Tell me what you cherish most. Give me the pleasure of taking it away.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Well, if you wanna go technical, it's y = ax^2+b thus -x is simply a=-1

Another funny one is y=a/x + b
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

ya lost me but if you want to get technical

http://www.dsmarketing.com/samples/mathsection.pdf

geek your heart out. 8)
Post Reply