Simple moving simulation?

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
systat
Posts: 41
Joined: Mon Jan 07, 2008 8:01 pm

Simple moving simulation?

Post 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);
								 }
											
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post 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.
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
dawasw
Posts: 357
Joined: Tue Aug 10, 2004 4:39 pm
Location: Poland

Post 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.)
systat
Posts: 41
Joined: Mon Jan 07, 2008 8:01 pm

Post 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...
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
systat
Posts: 41
Joined: Mon Jan 07, 2008 8:01 pm

Post 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".
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post 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:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Post Reply