Animation on Keypress

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Justin
Posts: 7
Joined: Thu Jan 01, 2009 8:04 pm

Animation on Keypress

Post by Justin »

Sorry for the bad englisch, it was translated by google ;) ...
Hello,
Apologies if this question is something very amateurish and quite possibly an answer exist should I should have missed.
I took the first tutorials on the off side viewed and Poe wanted this here: http://irrlicht.sourceforge.net/docu/example004.html modify something, ie I have a model with keyframe animation B3d format. I did this just now instead of the "cube" is loaded, so now this model is in the middle, can be left right and up and plays in a speed madness all keyframes behind each other. The speed I could without problems: node -> SetAnimationSpeed recruit, and with node -> SetFrameLoop I could keyframes to the "idle" are always play, but now when I hit w should be as long as the button is pressed Keyframes of 2-14 will be the model for "running" bring. I thought to myself so easy this SetFrameloop (2.14) inserted into the part in which the model also in the room is moved, but instead of the animation to play as long as the button is pressed, he now shows the first frame of the animation and Then she plays completely from when the button is released, until another "animation" by another button is ... how exactly is this now to solve, and where I am at the theme am, as I do regular, that the keyframe animation of a to b exactly will go through only once ...
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

You need to hold states for your character and use this to control the logic.

Code: Select all

enum E_CHARACTER_STATE
{
  ECS_STAND,
  ECS_WALK,
  ECS_JUMP,
  ECS_FALL,
};

E_CHARACTER_STATE PlayerState;
You can't walk if you are already walking, falling, jumping etc. Maybe you can walk if you are running.

When the key is pressed down:

Code: Select all

  if (PlayerState == ECS_STAND)
  {
     PlayerState = ECS_WALK;
     node->setFrameLoop(...);
  }
Key up:

Code: Select all

  if (PlayerState == ECS_WALK)
  {
     PlayerState = ECS_STAND;
     node->setFrameLoop(...);
  }
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Justin
Posts: 7
Joined: Thu Jan 01, 2009 8:04 pm

Post by Justin »

I solved the 2 problems that way, what do you think about it?

Code: Select all

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif

#include <irrlicht.h>
#include <iostream>

using namespace irr;
scene::IAnimatedMeshSceneNode* dwarf;
bool w_pressed = false;
bool schlag;

class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{

			//Keysettings
			if(event.KeyInput.Key == KEY_KEY_W && event.KeyInput.PressedDown == true && w_pressed==false)
			{
				dwarf->setFrameLoop(2,14);
				w_pressed = true;
			}
			else if(event.KeyInput.Key == KEY_KEY_W && event.KeyInput.PressedDown == false)
			{
				dwarf->setFrameLoop(292,325);
				w_pressed =false;
			}
			if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.PressedDown == true)
			{
				dwarf->setFrameLoop(112,126);
				schlag = false;
			}
			if(event.KeyInput.Key == KEY_KEY_A && event.KeyInput.PressedDown == false)
				schlag = true;

			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
		}
		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}
	
	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};


int main()
{
	// let user select driver type

	video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
	// create device
	MyEventReceiver receiver;

	IrrlichtDevice* device = createDevice(driverType,
			core::dimension2d<s32>(640, 480), 16, false, false, false, &receiver);

	if (device == 0)
		return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	dwarf = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/dwarf1.b3d"));
	if (dwarf)
	{
		dwarf->setPosition(core::vector3df(0,0,30));
		dwarf->setMaterialFlag(video::EMF_LIGHTING, false);
		dwarf->setFrameLoop(292,325);
		dwarf->setAnimationSpeed(15);
	}
	smgr->addCameraSceneNodeFPS(0, 100.0f, .1f);
	device->getCursorControl()->setVisible(false);

	int lastFPS = -1;

	u32 then = device->getTimer()->getTime();

	const f32 MOVEMENT_SPEED = 25.f;

	while(device->run())
	{
		const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
		then = now;

		core::vector3df nodePosition = dwarf->getPosition();

		if(receiver.IsKeyDown(irr::KEY_KEY_W))
			nodePosition.Z += MOVEMENT_SPEED * frameDeltaTime;
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			nodePosition.Z -= MOVEMENT_SPEED * frameDeltaTime;

		dwarf->setPosition(nodePosition);

		//Attack
		if(schlag==true)
		{
			int a = dwarf->getFrameNr();
			if(a == 125)
				dwarf->setFrameLoop(295,325);
		}

		driver->beginScene(true, true, video::SColor(255,113,113,133));

		smgr->drawAll(); // draw the 3d scene
		device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			core::stringw tmp(L"Movement Example - Irrlicht Engine [");
			tmp += driver->getName();
			tmp += L"] fps: ";
			tmp += fps;

			device->setWindowCaption(tmp.c_str());
			lastFPS = fps;
		}
	}

	/*
	In the end, delete the Irrlicht device.
	*/
	device->drop();
	
	return 0;
}

/*
That's it. Compile and play around with the program.
[/code]
Last edited by Justin on Fri Jan 02, 2009 9:42 am, edited 1 time in total.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Code: Select all

Don't forget putting that in a code tag...
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply