event realated ?

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
Dogs
Posts: 195
Joined: Wed Sep 15, 2004 1:04 am
Location: michigan

event realated ?

Post by Dogs »

Ok everyone hates talking about the same topics here but this one is a bit diff...
My code compiles great and seems to work one time lol..

Im working with event recivers and had simalar problem when doing my cursor and cam on and off settings...
The code loads one time and then never returns back to see my events when they accour...
Below is some code..



int spa = 100.0f;
int spb = 1000.0f;
int speed = spa;
int gravity = 0;
int gra = -40;

bool sp = true;
bool gr = true;


// the events below //

}

if ((event.EventType == EET_KEY_INPUT_EVENT) &&
!event.KeyInput.PressedDown){
switch(event.KeyInput.Key){
case KEY_SPACE:{
gr = !true;
}
}
}

if ((event.EventType == EET_KEY_INPUT_EVENT) &&
!event.KeyInput.PressedDown){
switch(event.KeyInput.Key){
case KEY_SHIFT:{
return sp = !true;
}break;
}
}
return false;
}
};




// the parts im wanting to control on the cam //
// cam //
// notice speed, and gravity //


if(gr){
gravity = gra;
}
if(sp){
speed = spb;
}
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,70.0f,(speed));
camera->setPosition(core::vector3df(7000,1100,4000));
camera->setFOV(1.0f);
camera->setFarValue(20000.0f);

scene::ISceneNodeAnimator* anim = 0;
anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,10,30),
core::vector3df(0,gravity,0), 100.0f,
core::vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();




Ok this code works to an extent..
If i set the gr,sp to true or false the if statements work..
But the code seems to only be making one pass over the events and not returning and then reloading the cam code when the changes are made by the events...
So even though the events may be working the cam code isnt being reloaded to reflect the changes...

How would i go about changing this to reload the cam code ?
maybe a while loop but then would the program be stuck in the loop and never move to the next section of code..?
I gues im just haveing a simple missunderstanding of how the flow of the program works and how to control it..

Any help on this I would be very thankfull...
Oh and this code is going to be changed a bunch more after I figure out how to get what ive got here working...
But the basic idea is to be able to togle from walking speed to running speed with the shift key.. Latter on im going to change this code by setting it up so that the event is only when the key pressed insted of toggling...
I dont think ill have any problems with mulitple keys being pressed like the arrows while walking and then pressing the shift key at the same time...
The gravity will be changed to only have a gravity setting durring any movement keys.. like any of the arrow keys and then when the key is released the gravity will be changed back to 0 as to not pull you down hills...
I latter on want to change this to be reflected with the triangle collision insted and as long as collision is present then the gravity will be 0 but anytime the collision isnt present then the gravity will set on again... This way lets say you jump from a top of a building as long as the character isnt colliding with anything then the gravity will be on and pull the character down and then when collision is present again then it will be set back to 0 again...

I hope everyone understands what im working on and also hope that it gives others the same kinda ideas in hopes to simplify using irrlicht and not to have to worry about incorporating physics of another sort...
It may be generic but it should work very well when I get it working... :)

Again thanks for any help on this...
Dogs
Posts: 195
Joined: Wed Sep 15, 2004 1:04 am
Location: michigan

Post by Dogs »

Hmm I figured that many people here that are good programmers could help out with this one...
Maybe it just cant be done...?
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

I'm not getting this part -

if ((event.EventType == EET_KEY_INPUT_EVENT) &&
!event.KeyInput.PressedDown){
switch(event.KeyInput.Key){
case KEY_SPACE:{
gr = !true;
}
}
}

if ((event.EventType == EET_KEY_INPUT_EVENT) &&
!event.KeyInput.PressedDown){
switch(event.KeyInput.Key){
case KEY_SHIFT:{
return sp = !true;
}break;
}
}
return false;
}
};


Specifically, why not something like this -

Code: Select all

switch ( event.EventType )
{
    case EET_KEY_INPUT_EVENT:
    {
        if ( event.KeyInput.Key == KEY_SPACE )
        {
            gr = false; // don't do gr = !true, [ unless you're intending to toggle the value of gr, I'll show how to do that below ] believe it or not, that takes slightly more processing, it causes the compiler to generate a temporary varaible on the stack, to hold the value of !true, then assigns that value to gr, then removes the temporary variable off the stack.  it's faster to just set it to true or false
            return true;
        }
        else if ( event.KeyInput.Key == KEY_SHIFT && event.KeyInput.PressedDown )
        {
            sp = !sp; // to toggle the value of sp, when the key is pressed, there's another problem here though, in that you need to handle the event so it's only processed once each time the key is pressed.  Right now this will constantly toggle the value of sp as long as the SHIFT key is pressed down
            return true;
        }
    }

    return false;
}
Dogs
Posts: 195
Joined: Wed Sep 15, 2004 1:04 am
Location: michigan

Post by Dogs »

Well i was going to toggle the values for temporary...
But they still dont work either way as ive tried them both ways...
My if statments are working if i change the sp, or gr values to true or false but i really dont think that program is reloading the cam code..
I do think its changeing the values in the events but the cam code just isnt being reloaded in order to reflect the changes...

Thats what im thinking... :)

I have no idea how to make this work so that when the changes are made to the cam section it reloads the cam section and changes are made...
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

I'm not sure what you mean by the cam code not reloading...

After you create your device, adding your eventReceiver, you then add you camera node. Once the camera node is added, you don't have to keep re-adding it, just modify it's position. With the collision animator, the position of the camera should follow the terrain ( or whatever node the selector represents ). The only things I notice that are a bit off, to me at least, with the camera code is the following -

camera->setFOV ( 1.0f );

This is making your view angle only 1 degree. This is not good, IMO. Best metaphor I can come up with this, is it's like looking through a coffee stirrer straw. FOV should be 45-60 degrees, I believe.

This one's nothing special, but your argument to the ellipsoidTranslation in the createCollisionResponseAnimator is saying that the ellipsoid which will be colliding with the selector in this animator is offset +50 units in the Y. With the Y radius of you're ellipsoid only being 10 units, the camera will be 40 units below the collision point. If you're idea is to use this to keep you "snapped" to the terrain, you will effectively be always 40 units below the terrain.

I'm also curious how you're creating the selector for the animator?

Ah, I think I get it now. You want to change the way the collision response animator is behaving, based on user input. So, what I'm guessing, is that you are trying to recreate the camera and the collision response animator when the user inputs those values.

I think what you want to do is, to leave the camera be. If the gravity is changed, from the input toggles, you'll want to do something like this -

Code: Select all

if ( gravityChanged )
{
    camera->removeAnimators ( );
    scene::ISceneNodeAnimator* anim = 0;
    anim = smgr->createCollisionResponseAnimator(selector, camera, core::vector3df(30,10,30), core::vector3df(0,gravity,0), 100.0f, core::vector3df(0,50,0)); 
    camera->addAnimator(anim); 
    anim->drop(); 
}
Notice you don't recreate the camera, you simply remove the animator, create the new one, with the changed gravity value, and then add it back to the camera. Once you understand this, then I can try to explain, how you would maintain the pointer to your animator, so you could remove the specific animator, in case you ever happen to have multiple animators assigned to the single camera.

Hope this answers your question.

EDIT: You'll also want to maintain the selector, so you don't have to keep re-creating it, everytime you re-create the collision response animator.
Dogs
Posts: 195
Joined: Wed Sep 15, 2004 1:04 am
Location: michigan

Post by Dogs »

Wow dude you are way off.. im sorry i didnt think my question was that confusing but ill exsplain agian...



core::vector3df(0,gravity,0), 100.0f,

The line above ive removed the gravity settings and replaced them with gravity insted...
I now want to be able to use an event reciver to toggle my gravity from -40 when a key is pressed back to 0 when the key is lifted..
I will use events for each movement key so that my gravity only pulls downward when the cameara is in motion and then is set to 0 and has no gravity when its not moving...

My code is working its changing the settings but for some reason the cam code seems to only load one time and that is when the application is started so no matter what the gravity is changed to while the app is running the cam code is never updated with the new gravity settings so it just stays the same all the time... at least thats what im seeing...
How can i get my event reciver to change the gravity settings on the fly and have them effect the application as it runs.. :(
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Well, after finding the ISceneNodeAnimatorCollisionResponse class, there is a function to setGravity. So, you'd set gravity to nothing when there is no movement, and then set the gravity to -40 when there is movement.

With what I was proposing above, I was thinking there was no way to change the gravity, but, after looking into it, you can.

The event receiver, should not create the camera, or the collision response animator, it should only set the gravity to whatever you want, based on keys being pressed, or not being pressed. The camera and the animator should only ever be created/added once!

If this isn't what you're looking for, then I have no clue what you are looking for! :x
Dogs
Posts: 195
Joined: Wed Sep 15, 2004 1:04 am
Location: michigan

Post by Dogs »

You are corect as to what i want to do..
I figured from what i read as you just found out about that you can change the gravity like i was attempting to do lol...
I actualy messed with your code and im shocked that it didnt work as after looking at it closely i changed a tad bit and was quite confident that it was going to work...

But lol it didnt...
And still my events are working as i can change them to Exit = true; and any key ive created events for, close the app just fine...
But still my boolean true and false settings are either not being seen by the program or the actual values of the gravity are not being noticed by the program when they are changed and just are staying the initial settings when the program was started...

So I can take and set my boolean values to true and or false and they work with my if statements when the program starts but when an etempt to change the values while the program is running those changes are not being reflected by the program and are stuck in there original state from when the program is started...

Its starting to kinda get to me lol..
Id like to figure out what i nead to do so that the program will either reload the cam section kinda like what you had proposed by removing the animator section and then inserting a new one with my changes...

If your code would have worked for me I would have just created two of those if statments and had one set for a key down event and one set for a key up event and or just set them for a togle system and all would have been fine..
I only want to turn my gravity on when the camera is being moved with the arrow keys as to keep the camera from flying around in space lol.. then set the gravity to 0 when any of the arrow keys are released and then the gravity will not slowly pull you downward as you stand still...

I didnt want to have to incorporate physics into the program as this is being a big enogh pain the butt for me lol... I dont nead to add to my suffering lol.. :(

But I will say this anyway when we figure this out it will be code im sure that many here would love to see and use for there own projects as there isnt anything i hate more is that haveing my character slowly slide down hills while tring to shoot at someone lol...
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Since all you're code is not posted, I'm assuming you are setting gr and sp to true right in OnEvent, before processing events? That's wrong. Also, this line in your original post -

if ( ( event.EventType == EET_KEY_INPUT_EVENT ) && !event.KeyInput.PressedDown )

if there was a key input event, and the key is NOT pressed down. And then you go on to check if they space key is the key being referenced, but you'll never get to the point where you check the space key. I think you want to do something like this -

CEventReceiver.h:

Code: Select all

#ifndef __CEVENTRECEIVER_H__
#define __CEVENTRECEIVER_H__

#include "Irrlicht.h"

using namespace irr;

class CEventReceiver : public IEventReceiver
{
public:
	CEventReceiver ( );

	virtual bool OnEvent ( SEvent event );

private:
	int		gravity;
	int		gra;
	bool	gr;
};

#endif // __CEVENTRECEIVER_H__
CEventReceiver.cpp:

Code: Select all

#include "CEventReceiver.h"

using namespace irr;

CEventReceiver::CEventReceiver ( )
{
	gravity = 0; 
	gra = -40; 
	gr = false;
}

bool CEventReceiver::OnEvent ( SEvent event )
{
	switch ( event.EventType )
	{
	case EET_KEY_INPUT_EVENT:
		{
			if ( event.KeyInput.Key == KEY_SHIFT && event.KeyInput.PressedDown )
			{
				if ( !gr )
				{
					gravity = gra;
					gr = true;
				}
	
				return true;
			}
			else
			{
				// Shift not being pressed, put gravity back to 0
				gravity = 0;
				gr = false;
			}
		}
	}
	
	return false;
}
Post Reply