Moving my character

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
TJBaldy
Posts: 16
Joined: Fri Dec 02, 2011 3:06 pm

Moving my character

Post by TJBaldy »

So I'm trying to implement the movement code from tutorial 4 into my own game, so that I can move my character forwards, left, etc etc. But for some reason the node (character) is moving by himself without any key pressing. Not sure what I've done wrong as I've compared my code to the tutorials code numerous times.

Code: Select all

 if (node)
                        {
                                    node->setMaterialFlag(EMF_LIGHTING, false);
                                    node->setPosition(core::vector3df(1,1,1));
                                    node->setMaterialTexture( 0, driver->getTexture("media/american_body.jpg") );
                        }       
And in my game loop;

Code: Select all

const u32 now = device->getTimer()->getTime();
                const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
                then = now;
 
                                core::vector3df nodePosition = node->getPosition();
 
                if(receiver.IsKeyDown(irr::KEY_KEY_W))
                        nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
                else if(receiver.IsKeyDown(irr::KEY_KEY_S))
                        nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
 
                if(receiver.IsKeyDown(irr::KEY_KEY_A))
                        nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
                else if(receiver.IsKeyDown(irr::KEY_KEY_D))
                        nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
 
               node->setPosition(nodePosition);
As I said, the code seems the same but the movement is happening itself. It's probably just a stupid error...

Thanks in advance :).
ScreenOfDeath
Posts: 21
Joined: Thu Dec 22, 2011 11:16 am
Location: Germany - NRW

Re: Moving my character

Post by ScreenOfDeath »

Hi,

just debug your code -> (Visual Studio) press F9 on the line where the "if cases" are.
Then, run your code,maybe the receiver is buggy, so the IsKeyDown always returns true - and the nodePosition will be increased.
I´m from germany ^^ So, if you find some errors , you can keep it!
TJBaldy
Posts: 16
Joined: Fri Dec 02, 2011 3:06 pm

Re: Moving my character

Post by TJBaldy »

Thanks for the quick reply. Do you mean use a breakpoint and use f9? Well I entered a breakpoint and it seems that you were right and it's true. Could I get round this problem by making the receiver false by default?
ScreenOfDeath
Posts: 21
Joined: Thu Dec 22, 2011 11:16 am
Location: Germany - NRW

Re: Moving my character

Post by ScreenOfDeath »

Ah, your are welcome.

Yes, its right - you shall use the breakpoints, and you can switch through your functions when you are press F11 ( step by step ) or F10 ( procedure step ) .

You shall make the receiver returns false as default -> because you dont pushed a key.

Here is a small example to use a better code style:

You "have to" code always OO - ( object oriented )

Maybe something like this:

Code: Select all

 
 
 
CEntity.h
 
class CEntity
{
private: 
int _Health;
bool _Move;
// Your GameManager
// or Irr Pointers...
 
void updatePosition(); // updates your position
// some more update procedures
void move();
 
public:
CEntity(???); // some more...
ISceneNode* SceneNode;
void command( ??? , ???); // You have to implement this for "global" commands -> would be easier if you are using someday an AI - System.
// Physic actor ?
// some other stuff...
 
 
void update(); // Update your all of the private update procedures
}
 
 

Code: Select all

 
CEntity.cpp
 
void CEntity::command(???,???)
{
switch (???)
{
case EC_MOVE_TURN:
{
this->turn(???);
break;
}
case EC_MOVE:
{
this->move(???);
break;
}
 
}
};
 
 
CEntity::updatePosition()
{
// get the position of the physic object..
// in this case you only want the position of your scenenode
 vector3df nodePosition = SceneNode->getPosition();
 
vector3df newPos = ???// Your calculation
SceneNode->setPosition(newPos);
}
 

Code: Select all

 
KeyboardUpdate:
 
void keyboardUpdate()
{
        vector3df vel = vector3df(0,0,0);
 
        if (KeyEventReceiver.isKeyDown(KEY_KEY_W))      vel +=vector3df(-1,0,-1);
        if (KeyEventReceiver.isKeyDown(KEY_KEY_S)) vel += vector3df(1,0,1); 
 
        if (KeyEventReceiver.isKeyDown(KEY_KEY_D)) Entity->command(EC_MOVE_TURN,vector3df(0,+1,0)); // Entity is your CEntity - which you created before with CEntity *Entity = new CEntity();
        if (KeyEventReceiver.isKeyDown(KEY_KEY_A)) Entity->command(EC_MOVE_TURN,vector3df(0,-1,0));
 
        if (vel.getLength()) Entity->command(EC_MOVE,vel); // Where command your own procedure is, which calls the private move function or something else
}
 


That shall be a small example how to write it OO correctly - but I think you are really new and only want to see what happends if you are change some variables and add some code to the examples.
If I´m wrong, you shall learn how to code it right - please look at tutorials and code some very basic "games" or apps - it will help you to understand things better.
I´m from germany ^^ So, if you find some errors , you can keep it!
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Moving my character

Post by randomMesh »

1. Does it happen with the binary which comes with the SDK too?
2. Does it happen when you compile example 4 without changes?
3. Make sure you didn't forget the constructor of MyEventReceiver which initializes all booleans to false:

Code: Select all

 
MyEventReceiver()
{
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                KeyIsDown[i] = false;
}
 
"Whoops..."
TJBaldy
Posts: 16
Joined: Fri Dec 02, 2011 3:06 pm

Re: Moving my character

Post by TJBaldy »

randomMesh wrote:1. Does it happen with the binary which comes with the SDK too?
2. Does it happen when you compile example 4 without changes?
3. Make sure you didn't forget the constructor of MyEventReceiver which initializes all booleans to false:

Code: Select all

 
MyEventReceiver()
{
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                KeyIsDown[i] = false;
}
 
I did actually forget to put this code in. But even with it, I still have the bug where my node is moving automatically. The funny thing is, the node is moving diagonally upwards rather than forwards or backwards etc.

When I did tutorial 4 I didnt have any problems and the movement was exactly what I expected.

However, I do have some other code within my KeyEventReciever which may be conflicting with this code, but I'm not sure. It's code from the terrain tutorial which looks for an "event".
TJBaldy
Posts: 16
Joined: Fri Dec 02, 2011 3:06 pm

Re: Moving my character

Post by TJBaldy »

Ok ignore my stupidity, I just realised that I was using the Y values to change the direction of the node which is why it's going upwards, not because of the bug. However, the bug still exists. I've changed the Y values to Z's now so thats ok.

I've noticed that when I try and control the node myself (with the key presses myself), the runtime bug stops and I can control the node myself. Therefore, the bug only runs once when the program executes. Just gotta stop that from happening now :S.

Thanks for everyones help so far too :)
Post Reply