Irrlicht, Bullet and raycast car

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
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

Irrlicht, Bullet and raycast car

Post by enif »

Hi!

I am trying to create a car racing game. A simple one. :-) I have red a lot about Irrlicht and Bullet here. I am plying with the Bullet's vehicle demo. I really do something for it... :-) My problem is, an I am kindly asking you for a help, that with simple object I have no problem, but the raycast car in Bullet is giving me hard times over a week. And I am clueless now.

The raycast car is allways falling throught surfaces. Only the chassis hits the ground. And if I am applying a force to it's wheels, nothing is happening - no turning, no rotation.

So, please, can anybody help me with that? Here are complete sources (VS2008) of the demo app. Hopefully, somebody will be able to fix it. And we will have a nice tutorial about raycast vehicles.

Please focus on PhysicsCar.h and PhysicsCar.cpp files in BulletAnimator directory.

http://www.megaupload.com/?d=5FDG6FCX (the sources)

Here are some influential threads (thanks!):

http://irrlicht.sourceforge.net/phpBB2/ ... ght=bullet
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=22764


Thanks to all and sorry for my bad English!

PS: I am using Irrlicht 1.6 and Bullet 2.75.
cobra
Posts: 371
Joined: Fri Jan 23, 2009 2:56 am
Location: United States
Contact:

Post by cobra »

What I've learned from implementing the bullet vehicle into Irrlicht:

Make sure the wheels' cast points are all well above your mesh, or else you will tunnel through things.

Mess with parameters of the wheels. It took me a while to find the correct parameter settings.

Also, try setting up a debug drawer derived from btDebugDraw, so you can actually see what is going on. I managed to do it without the debugger, but I used one now. Maybe that could narrow it down for us a bit more.

btw: I've already started a nice Bullet wrapper for Irrlicht, but I took a break from it and working on my flight simulator for a while, and the wrapper will definitely take second place. I will add the vehicle support for it the best I can, but not any time soon.
Josiah Hartzell
Image
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

Post by enif »

cobra: Thanks!

I'll try to experiment with wheels and other parameters. Hopefully I'll get somewhere. I am little bit confused with the results though - I tryed exactly the same parameters, like in the Bullet's vehicle demo, without any success. I am sure, that it is me, ho is wrong here, but... :-)

And I'll try to create the debug drawer.


Have nice day,

enif
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

Updated - btIDebugDraw

Post by enif »

Hi!

in today's version, I added btIDebugDraw object. Now we can see, what Bullet is seeing. Problem with te car is not fixet yet, but there is something, that may help.

See this links:

http://sio2interactive.forumotion.net/s ... d-t599.htm
http://irrlicht.sourceforge.net/phpBB2/ ... idebugdraw

The sources: http://www.megaupload.com/?d=OCVAY4IH

Thanks to Francescu and pc0de!

EDIT:

There is a bug in CBulletPhysics.cpp file. Please use this new version of BulletToIrr() function:

Code: Select all

void BulletToIrr(const btVector3& in, core::vector3df& result)
{
    //result.set(in.getX(), in.getY(), -in.getZ());
	result.set(in.getX(), in.getY(), in.getZ());
} 

enif
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

a fix to debug drawer

Post by enif »

I found a bug in my debug drawer. Or better say - inconsistency in colors returned by Bullet :-). Sometimes they are in 0..1 range, sometimes in 0..255 range.

This is a fix for CBulletPhysics.cpp (see the end of the file):

Code: Select all

void btDebugDrawer::drawLine(const btVector3& from,const btVector3& to,const btVector3& fromColor, const btVector3& toColor)
		{
			//glBegin(GL_LINES);
			//	glColor3f(fromColor.getX(), fromColor.getY(), fromColor.getZ());
			//	glVertex3d(from.getX(), from.getY(), from.getZ());
			//	glColor3f(toColor.getX(), toColor.getY(), toColor.getZ());
			//	glVertex3d(to.getX(), to.getY(), to.getZ());
			//glEnd();

			//printf(">> TDebugNode::drawLine\n");

			if (m_debugNode != NULL)
			{
				core::vector3df v1, v2;
				BulletToIrr(from, v1);
				BulletToIrr(to, v2);

				// this solves the color bug in btRaycastVehicle
				btScalar r = fromColor.x();
				if (r <= 1) r *= 255;  // 0..1 to 0..255

				btScalar g = fromColor.y();
				if (g <= 1) g *= 255;  // 0..1 to 0..255

				btScalar b = fromColor.z();
				if (b <= 1) b *= 255;  // 0..1 to 0..255

				// handle bullet simplex debug color bug...
				//video::SColor icolor(255, (u32)fromColor.x() * 255.0, (u32)fromColor.y() * 255.0, (u32)fromColor.z() * 255.0);
				video::SColor icolor(255, (u32)r, (u32)g, (u32)b);

				video::S3DVertex vert1(v1, v1, icolor, core::vector2df());
				video::S3DVertex vert2(v2, v2, icolor, core::vector2df());
			
				m_debugNode->addLine(vert1, vert2); 
			}
		}
link3rn3l
Posts: 81
Joined: Wed Nov 15, 2006 5:51 pm

Post by link3rn3l »

Thanks enif for Terrain fixes in Bullet animator.

there send the raycast preview with some bugs..

http://www.megaupload.com/?d=K4JFGWRX





Happy Christmas to Everyone!!!
Bennu (Best 2d and 3D dev-tool)
http://bennupack.blogspot.com

Pixtudio (Best 2D development tool)
http://pixtudiopack.blogspot.com

Bennu3D(3D Libs for bennu)
http://3dm8ee.blogspot.com/

Colombian Developers - Blog:
http://coldev.blogspot.com/
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

Post by enif »

link3rn3l: Thanks a lot! You are the man! :-)

I finally fixed my car. It is working now, but I do not understand why yet. I am using box shapes for collisions, but they behave very differently, depending on usage. I'll dig into this and, hopefully, find a solution.

I'll spend this weekend on the PhysCar.cpp, clean up it and post the final version. There is too much testing code and harcoded values in it.

Here are some descriptions and parameters for a car. (Based on knowledge gained here on the forum - thanks goes to Kester Maddock for his "Vehicle Simulation With Bullet" paper).

Code: Select all

// The coefficient of friction between the tyre and the ground. 
// Should be about 0.8 for realistic cars, but can increased for better handling.
// Set large (10000.0) for kart racers
m_wheelFriction = 10; //BT_LARGE_FLOAT;

// The stiffness constant for the suspension. 
// 10.0 - Offroad buggy, 50.0 - Sports car, 200.0 - F1 Car
m_suspensionStiffness = 10.f;

// The damping coefficient for when the suspension is expanding. 
// See the comments for m_wheelsDampingCompression for how to set k.
// m_wheelsDampingRelaxation should be slightly larger than m_wheelsDampingCompression, eg 0.2 to 0.5
m_suspensionDamping = 0.2f;

// The damping coefficient for when the suspension is compressed. 
// Set to k * 2.0 * btSqrt(m_suspensionStiffness) so k is proportional to critical damping.
// k = 0.0 undamped & bouncy, k = 1.0 critical damping
// 0.1 to 0.3 are good values
m_suspensionCompression = 0.1f;

// The maximum length of the suspension (metres)
m_suspensionRestLength = 0.1f * PHYSCAR_SCALE;

// Reduces the rolling torque applied from the wheels that cause the vehicle to roll over.
// This is a bit of a hack, but it's quite effective. 0.0 = no roll, 1.0 = physical behaviour.
// If m_frictionSlip is too high, you'll need to reduce this to stop the vehicle rolling over.
// You should also try lowering the vehicle's centre of mass
m_rollInfluence = 1.0f; 
Note: The car model is from this demo: http://www.codeproject.com/KB/game/IRRCarSim.aspx

It is modeled in a specific way (dictated by the IPhysics library), so it has to be rotated in a 3D editor, before used. No rotation in game will be needed.

Thanks again, I'll publish the final/fixed version next week.
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

The bug found? :-)

Post by enif »

Hi!

I found the line of crap, that made my days sad...

All happened in the void CPhysicsObject::initPhysics(void) method. I misused the filtergroup and filtermask parametres. Poor raycast vehicle was in a different filtergroup with a different filtremask. No wonder, that it can not hit other objects in the world! So many time wasted. I promise, I start to read the manuals! :-)

Code: Select all

// the bad one
m_physicsWorld->getDynamicsWorld()->addRigidBody(m_rigidBody, m_filterGroup, m_filterMask);
			
// the good one
m_physicsWorld->getDynamicsWorld()->addRigidBody(m_rigidBody);

Happy Christmas to all good souls out there!
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

Preview

Post by enif »

Here is a preview of the final version.

http://www.megaupload.com/?d=5A2XU6PB

- car model is rotated correctly
- left wheels are rotated by 180 degrees around the Y axis to face the left side of the car
- physics parameters set to some interesting values
- car body has it's body mass center shifted for beter handling
- the PhysicsCar.cpp and .h cleaned up a bit

If you want to use your own car, make sure, that is front is facing Z+ axis and its top is towards Y+ axis. -X is on the left, +X is on the right.


By!
link3rn3l
Posts: 81
Joined: Wed Nov 15, 2006 5:51 pm

Re: Preview

Post by link3rn3l »

enif wrote:Here is a preview of the final version.

http://www.megaupload.com/?d=5A2XU6PB

- car model is rotated correctly
- left wheels are rotated by 180 degrees around the Y axis to face the left side of the car
- physics parameters set to some interesting values
- car body has it's body mass center shifted for beter handling
- the PhysicsCar.cpp and .h cleaned up a bit

If you want to use your own car, make sure, that is front is facing Z+ axis and its top is towards Y+ axis. -X is on the left, +X is on the right.


By!

Thanks ..!!!! :D
Bennu (Best 2d and 3D dev-tool)
http://bennupack.blogspot.com

Pixtudio (Best 2D development tool)
http://pixtudiopack.blogspot.com

Bennu3D(3D Libs for bennu)
http://3dm8ee.blogspot.com/

Colombian Developers - Blog:
http://coldev.blogspot.com/
enif
Posts: 12
Joined: Sun Nov 15, 2009 5:31 pm
Location: Prague, Czech Republic

New version

Post by enif »

link3rn3l: You are welcome! ;-)

Here is a new version:

- car settings are moved to a separate struct (see SPhysicsCarParams.h)
- brakes are applied to all wheels with different strength on each axis (needs to be configurable by SPhysicsCarParams struct and in realtime)
- engine power can be applied to different set of wheels (front, rear, 4x4) - see comments in PhysicsCar.cpp

http://www.megaupload.com/?d=PLDVXS1J

It is not finished yet. And there is somethnig bad smelling - see the compiled demo. The car is moving smoothly, but the bullet debug geometry is "jumpy". I checked out the bullets's debug drawers and they are using worldTransform.getOrigin() as a base for vectors send to the drawer. The same is applied to the irrlicht geometry in my demo. The result is strange...

By!
personoid
Posts: 12
Joined: Fri Oct 22, 2010 7:14 am

Post by personoid »

Has any further work been done on this demo?

I was able to get it compiling and running with the latest Irrlicht (1.71) and Bullet (2.77).

I'm interested in how the car parameters (eg supspension settings) were chosen , and how to tweak them.

Thanks

Personoid.
Post Reply