Page 1 of 1

Simple moving simulation?

Posted: Sat Jul 05, 2008 2:47 pm
by systat
I use next snippet to move my car, but it is really really bad, it is only moving through X and Z ose, and it looks like you are dragging box, also when I keep pressed 2 buttons at the same time it is not working properly.
Here is this pore, simplest snippet,can someone show me better way to control your car, for example if I press W and A at the same time it should go in circles, etc...

Code: Select all

	switch (event.KeyInput.Key)
			{
			case irr::KEY_KEY_W: {	
				v = node->getPosition();
					v.Z-= 75.0f;
					node->setPosition(v);
								 }
				return true;
			case irr::KEY_KEY_S: {
				v=node->getPosition();
				v.Z+=75.0f;
				node->setPosition(v);
								 }
								 return true;
			case irr::KEY_KEY_A: {
                 v=node->getPosition();
				v.X+=75.0f;
				node->setPosition(v);
								 }
								 return true;
								 case irr::KEY_KEY_D: {
                 v=node->getPosition();
				v.X-=75.0f;
				node->setPosition(v);
								 }
											

Posted: Sat Jul 05, 2008 4:15 pm
by shadowslair
In my opinion this code is from the Event Receiver, which I personally dislike.

You can easily use code like this (Where Key_1 is "W", Key_2 is "S", Key_3 is "A" and Key_4 is "D".)

Code: Select all

position_vector.Z+= (Key_1-Key_2)*some_adjust_value;
Rotation_Pan+= (Key_3-Key_4)*some_adjust_value_2;
Some or lots of bugs may apper if U just Copy-Paste it.
This is just a simple in-hurry example. Sorry, but currently I`m far too lazy. You got the idea, right?

PS: Ah, and U can get the "V" value before the Switch statement, so not to call it four times.

PS2: For me a value of type X+=75 units for each step is far too big value. Scale down everything. Imagine you have a 5 square kilometers level. Your CPU will have to work with far too long values, which I dislike too.

Posted: Sat Jul 05, 2008 8:38 pm
by dawasw
My suggestion is to use Newton physics or Bullet. (For example.)

Search the forums for newton car examples - some are really nicely done. (I mean - you can see how to implement all the needed stuff and setup a car with Newton in Irrlicht for example.)

Posted: Sat Jul 05, 2008 8:51 pm
by systat
OK, anyway I wonder how can I make possible that I keep pressed down 'W' and lets say 'A' at the same time...
And that simultaneously 'W' will do its function, and 'A' its own function.
When I now lets say press down 'W', and the press 'A', everything will stop(even the movement that I had with 'W') and then I need to press 'A' to continue...

Posted: Sat Jul 05, 2008 10:24 pm
by rogerborg
Wild guess: are you working with "nel" on this?

OK, same advice to you:

The event receiver receives changes in key states. It doesn't get repeated events when a key is held down.

So use the idiom in the SDK example 04.Movement. Have your event receiver store key states, and in your main loop, check the state of the keys that you're interested in.

All that said, your objects should move every time round your main loop regardless of whether or not any keys are down. Key presses should change the speed at which the objects move, rather than move the objects directly.

Posted: Sun Jul 06, 2008 10:08 am
by systat
Nice, but I am newbie, the only "idiom" I saw in movement example, was this

Code: Select all

              case  KEY_KEY_W:
			case KEY_KEY_S:
				{
					core::vector3df v = node->getPosition();
					v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node->setPosition(v);
				}
				return true;
So you think that my event receiver will work fast if I change my code to this, I am not so sure, because I have same problem with this example considering unsatisfactory input.
I don't know how to check key states of my keys, can't you show me some snippet, I will work around it myself...
I don't know who is "nel".

Posted: Sun Jul 06, 2008 4:10 pm
by shadowslair
All the Tutorials are made nicely, so take a look once again.
So you think that my event receiver will work fast if I change my code to this
"Fast" is a strange word, while talkin for so basic calculations like those. Creaying a car game is far too difficult I think, and will never be finished the way you want it, unless you use any physics engine.

So my advice is to examine the scripts in the tutorials until everything is clear. Believe me, they are descently made and optimised, so learn form there.

Sorry for disappointing you with not posting the code you want, but I cannot write it down this way. It depends on lots of things. After lerning the basics, you can use some ready snippet as somebody mentioned already. I`m sure "learing in action" is a good strategy to use, but it will be more effective after the basics. :wink:

Good luck! :wink: