Jumping CollisionResponceAnimator

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
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Jumping CollisionResponceAnimator

Post by JPulham »

This is a modification to CollisionResponseAnimator to add Jumping for FPS.
Maybe Someone *hint to Niko* could add it to the REAL CollisionResponceAnimator :D .
Anyway, most of it is Copied from the Current version of CollisionResponseAnimator... so alot of code isn't linked to jumping.
CJumpingAnimator.h

Code: Select all

#ifndef __C_SCENE_NODE_ANIMATOR_JUMPING_COLLISION_RESPONSE_H_INCLUDED__
#define __C_SCENE_NODE_ANIMATOR_JUMPING_COLLISION_RESPONSE_H_INCLUDED__

#include "ISceneNodeAnimatorCollisionResponse.h"
#include "ITimer.h"

namespace irr
{
namespace scene
{
    //Basically CSceneNodeAnimatorCollisionResponse but with Jumping...
	class CSceneNodeAnimatorJumpingCollisionResponse : public ISceneNodeAnimatorCollisionResponse
	{
	public:

		//! constructor
		CSceneNodeAnimatorJumpingCollisionResponse(ISceneManager* scenemanager,
			ITriangleSelector* world, ISceneNode* object,
			const core::vector3df& ellipsoidRadius = core::vector3df(30,60,30),
			const core::vector3df& gravityPerSecond = core::vector3df(0,-100.0f,0),
			const core::vector3df& ellipsoidTranslation = core::vector3df(0,0,0),
			f32 slidingSpeed = 0.0005f, ITimer* Timer = 0);

		//! destructor
		virtual ~CSceneNodeAnimatorJumpingCollisionResponse();

		//! Returns if the attached scene node is falling, which means that
		//! there is no blocking wall from the scene node in the direction of
		//! the gravity.
		virtual bool isFalling();

		//! Sets the radius of the ellipsoid with which collision detection and
		//! response is done.
		virtual void setEllipsoidRadius(const core::vector3df& radius);

		//! Returns the radius of the ellipsoid with wich the collision detection and
		//! response is done.
		virtual core::vector3df getEllipsoidRadius() const;

		//! Sets the gravity of the environment.
		virtual void setGravity(const core::vector3df& gravity);

		//! Returns current vector of gravity.
		virtual core::vector3df getGravity() const;

		//! Sets the translation of the ellipsoid for collision detection.
		virtual void setEllipsoidTranslation(core::vector3df translation);

		//! Returns the translation of the ellipsoid for collision detection.
		virtual core::vector3df getEllipsoidTranslation() const;

		//! Sets a triangle selector holding all triangles of the world with which
		//! the scene node may collide.
		virtual void setWorld(ITriangleSelector* newWorld);

		//! Returns the current triangle selector containing all triangles for
		//! collision detection.
		virtual ITriangleSelector* getWorld() const;

		//! animates a scene node
		virtual void animateNode(ISceneNode* node, u32 timeMs);

		//! My code... for jumping:
		virtual void jump(const core::vector3df& force, const core::vector3df& change);

	private:

		core::vector3df LastPosition;
		core::vector3df Radius;
		core::vector3df CurrGravity;
		core::vector3df Change;
		core::vector3df Gravity;
		core::vector3df Translation;

		ITriangleSelector* World;
		ISceneNode* Object;
		ISceneManager* SceneManager;
		ITimer* Timer;
		u32 LastTime;
		u32 FallStartTime;
		u32 LastGTime;
		f32 SlidingSpeed;
		bool Falling;

		core::triangle3df RefTriangle;
	};

} // end namespace scene
} // end namespace irr


#endif
CJumpingAnimator.cpp:

Code: Select all

#include "CJumpingAnimator.h"
#include "ISceneCollisionManager.h"
#include "ISceneManager.h"

namespace irr
{
namespace scene
{

//! constructor
CSceneNodeAnimatorJumpingCollisionResponse::CSceneNodeAnimatorJumpingCollisionResponse(
		ISceneManager* scenemanager,
		ITriangleSelector* world, ISceneNode* object,
		const core::vector3df& ellipsoidRadius,
		const core::vector3df& gravityPerSecond,
		const core::vector3df& ellipsoidTranslation,
		f32 slidingSpeed, ITimer* Timer)
: SceneManager(scenemanager), World(world), Object(object),
	Radius(ellipsoidRadius), Gravity(gravityPerSecond / 1000.0f), CurrGravity(gravityPerSecond / 1000.0f),
	SlidingSpeed(slidingSpeed), Translation(ellipsoidTranslation)
{
	if (World)
		World->grab();

	if (Object)
		LastPosition = Object->getPosition();

	Falling = false;

	LastTime = Timer->getTime();
	FallStartTime = LastTime;

	RefTriangle.pointA.set(0.0f, 0.0f, 0.0f);
	RefTriangle.pointB.set(0.0f, 0.0f, 0.0f);
	RefTriangle.pointC.set(0.0f, 0.0f, 0.0f);
}



//! destructor
CSceneNodeAnimatorJumpingCollisionResponse::~CSceneNodeAnimatorJumpingCollisionResponse()
{
	if (World)
		World->drop();
}


//! Returns if the attached scene node is falling, which means that
//! there is no blocking wall from the scene node in the direction of
//! the gravity.
bool CSceneNodeAnimatorJumpingCollisionResponse::isFalling()
{
	_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
	return Falling;
}


//! Sets the radius of the ellipsoid with which collision detection and
//! response is done.
void CSceneNodeAnimatorJumpingCollisionResponse::setEllipsoidRadius(
	const core::vector3df& radius)
{
	Radius = radius;
}

//! Returns the radius of the ellipsoid with wich the collision detection and
//! response is done.
core::vector3df CSceneNodeAnimatorJumpingCollisionResponse::getEllipsoidRadius() const
{
	return Radius;
}


//! Sets the gravity of the environment.
void CSceneNodeAnimatorJumpingCollisionResponse::setGravity(const core::vector3df& gravity)
{
	Gravity = gravity;
}


//! Returns current vector of gravity.
core::vector3df CSceneNodeAnimatorJumpingCollisionResponse::getGravity() const
{
	return Gravity;
}


//! Sets the translation of the ellipsoid for collision detection.
void CSceneNodeAnimatorJumpingCollisionResponse::setEllipsoidTranslation(core::vector3df translation)
{
	Translation = translation;
}



//! Returns the translation of the ellipsoid for collision detection.
core::vector3df CSceneNodeAnimatorJumpingCollisionResponse::getEllipsoidTranslation() const
{
	return Translation;
}


//! Sets a triangle selector holding all triangles of the world with which
//! the scene node may collide.
void CSceneNodeAnimatorJumpingCollisionResponse::setWorld(ITriangleSelector* newWorld)
{
	if (World)
		World->drop();

	World = newWorld;
	if (World)
		World->grab();
}



//! Returns the current triangle selector containing all triangles for
//! collision detection.
ITriangleSelector* CSceneNodeAnimatorJumpingCollisionResponse::getWorld() const
{
	return World;
}



//! animates a scene node
void CSceneNodeAnimatorJumpingCollisionResponse::animateNode(ISceneNode* node, u32 timeMs)
{
	if (node != Object)
	{
		return;
	}

	if (!World)
		return;

	u32 diff = timeMs - LastTime;
	LastTime = timeMs;
	LastGTime = timeMs;

	if(CurrGravity != Gravity)
	{
        if(CurrGravity.Y > Gravity.Y)
        {
            CurrGravity.Y += Change.Y;// * (f32)diff;
        }
        if(CurrGravity.Y < Gravity.Y)CurrGravity = Gravity;
	}

	core::vector3df pos = Object->getPosition();
	core::vector3df vel = pos - LastPosition;
	core::vector3df g = CurrGravity;// * (f32)diff;

	if (Falling)
      g = CurrGravity * (f32)((timeMs - FallStartTime) * diff);

	core::triangle3df triangle = RefTriangle;

	if (vel+g != core::vector3df(0,0,0))
	{
		// TODO: divide SlidingSpeed by frame time

		bool f = false;
		pos = SceneManager->getSceneCollisionManager()->getCollisionResultPosition(
				World, LastPosition-Translation,
				Radius, vel, triangle, f, SlidingSpeed, g);

		pos += Translation;

		if (f)//triangle == RefTriangle)
		{
			if (!Falling)
				FallStartTime = timeMs;

			Falling = true;
		}
		else
			Falling = false;

		Object->setPosition(pos);
	}

	LastPosition = Object->getPosition();
}

//This is where I start:

void CSceneNodeAnimatorJumpingCollisionResponse::jump(const core::vector3df& force, const core::vector3df& change)
{
    CurrGravity = force / 1000.0f;
    Change = change / 1000.0f;
}

} // end namespace scene
} // end namespace irr
Its used like this:

Code: Select all

if(GameReceiver.getKeyState(KEY_SPACE) && !g_PhysicsAnim->isFalling())
{
    g_PhysicsAnim->jump(vector3df(0,20,0),vector3df(0,-0.5,0));
}
pushpork
klikmaster
Posts: 40
Joined: Mon Sep 11, 2006 1:06 pm

Post by klikmaster »

I havn't tried it, but good idea, although isn't the collisionresponsanimator for collisions? :P I see where you're coming from though, as it links in quite nicely.

Also, I wanted to say hello fellow 'klikker' :wink:
~IRRLICHT ROX MY SOX~
Elazul
Posts: 38
Joined: Fri Mar 23, 2007 4:47 pm

Post by Elazul »

this is a stupid question, but where would i put
CJumpingAnimator.h
and
CJumpingAnimator.cpp ?

i've tried putting them in the project folder and they don't work..
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

you add them to your project, in the project directory... what problems are you getting?
pushpork
Elazul
Posts: 38
Joined: Fri Mar 23, 2007 4:47 pm

Post by Elazul »

i'm hash including them into the code, and yet it will not allow me to include them.

i tried doing that with other .h and .cpp files (that i made before) and it worked.
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

You add the .cpp to your project to compile, and you put the .h in the same directory and #include it.
pushpork
MenthalMan
Posts: 1
Joined: Wed May 02, 2007 8:32 am
Location: Germany

Post by MenthalMan »

What do i have to add into my code to use the JumpingCollisionResponse?
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

First, add

Code: Select all

#include "CJumpingAnimator.h"
next add CJumpingAnimator.cpp to your project (make it one of the files you compile)

these files are in the post above.

now when you create a colision responce animator do this:

Code: Select all

ISceneNodeAnimator *anim = new CSceneNodeAnimatorJumpingCollisionResponse(scenemanager, world, scenenode);
now when ever you want to jump call (once per jump)

Code: Select all

anim->jump(jumpforce,gravity); 
pushpork
Urr
Posts: 1
Joined: Mon Sep 07, 2015 2:29 am

Re: Jumping CollisionResponceAnimator

Post by Urr »

Hello,

New to this forum.


Just wanted to share this hackish but nicely working jump code, this is from my game, "Graviton 3d"...


//I created a public Boolean key array that mirrors the key events to allow this type of code anywhere
if(KEYS[32]==true)
{
camera->setPosition((camera->getPosition())+vector3df(0.0f,7.0f,0.0f)); //add to vertical component of fps camera position to offset gravity
//jump key vector addition trick works well
}else{
//this could have other functions to sense for grab or other animation while falling etc...
}

//endjump code
JLouisB
Posts: 67
Joined: Tue Jul 24, 2012 12:36 pm
Location: France

Re: Jumping CollisionResponceAnimator

Post by JLouisB »

You can also use the CollisionResponse animator it has now a jump method.

About this, CuteAlien, if you read the topic, what about these patchs ?
http://irrlicht.sourceforge.net/forum/v ... p?p=191177
They are easy to check, and there are the test case compiled on Windows with Irrlicht with and without the patch.
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Jumping CollisionResponceAnimator

Post by CuteAlien »

I can put it on my todo ... but I start to feel that that is becoming a running joke. Sorry, 5th new item on it last 2 weeks and I already learned I will spend next free weekend I can find with building another release because last one had a big bug. I'll try to get to it - but will likely take a while.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply