Modeling a train using joints & a number of raycast vehicles

dumbo2007
Posts: 66
Joined: Sun Jan 30, 2011 8:46 pm

Modeling a train using joints & a number of raycast vehicles

Post by dumbo2007 » Wed Nov 23, 2011 2:47 pm

Hi,

I am trying to model a train in Bullet using a number of raycast vehicles linked using btHingeConstraint

http://www.youtube.com/watch?v=YFqkDUyERFE


The chassis looks kind of funny, it was the fastest shape I could come up with to link a number of vehicles together :D

So the issue is the rail has to be quite high on both sides to keep the vehicles within the line. At high speeds, the wheels of the ray cast vehicles climb on the rail if the rails are low and this derails the train :(

If I raise the height of the rails quite some more, its fine, but the entire thing does not look correct. The raycast vehicle's wheels should stay within the rails even at high speed.

I think what is happening is one of the ray casts is detecting the higher rail beneath the wheel in some sim step and positioning the wheel above the rail. Can this be suppressed somehow and the wheel be forced to stay at a particular height above the ground ?

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by Flix » Fri Nov 25, 2011 12:54 pm

dumbo2007 wrote:Can this be suppressed somehow and the wheel be forced to stay at a particular height above the ground ?
One question: if you remove the railway and run the demo, do you experience any change?
As far as I know the only solution is to make the rails higher than the bottom of the train chassis, and higher than the "raycast origin" too.

You can make the raycasts ignore the rails if you want (they should accept a collision filter mask, see this post http://bulletphysics.org/Bullet/phpBB3/ ... t=vehicles), but I'm not sure that this will make things better... and I'm wondering how you plan to make the train follow a curved rail too...
Another solution is to use non-raycast vehicles. I've made a Bullet demo about it some year ago http://bulletphysics.org/Bullet/phpBB3/ ... t=vehicles, and I placed a train in it.

However raycast vehicles should be better for performance, so if you can find a way to build trains with them please let me know :)

dumbo2007
Posts: 66
Joined: Sun Jan 30, 2011 8:46 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by dumbo2007 » Fri Nov 25, 2011 1:17 pm

if you remove the railway and run the demo, do you experience any change?
Well it goes smoothly for some distance but then suddenly loses direction and goes haywire :P

So thanks for the pointer to your demo thread, I ll check it out. I am thinking the best solution would probably be to make a
single raycast vehicle follow a track by hacking the current code. The current raycast implementation shoots rays from the connection point of the wheels, downwards(I think).

If a ray is also shot outwards from the connection point, towards the outer edge of the wheel(at a slightly higher angle to ensure it hits the track), then this can be used to get the track position on both sides. Then I ll make sure the wheel is always positioned in between the rails.

If there is any attempt to position the wheels above the rails, that can probably be detected and cancelled :D Its probably due to the wheels being pushed slightly outwards and consequently the raycast from the wheel connection point detecting the rail underneath it and then pushing the wheel above it.

This will work for curving tracks also.

Image

If the curving track is made from a bunch of cubes places in a curving path, then the chassis has to be oriented accordingly, while keeping the front and back wheels within the nearest sections(cubes) of the tracks

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by Flix » Sun Nov 27, 2011 8:16 am

dumbo2007 wrote:If a ray is also shot outwards from the connection point, towards the outer edge of the wheel(at a slightly higher angle to ensure it hits the track), then this can be used to get the track position on both sides.
This is an interesting idea... the chassis should stay inside the rails, but I'm not completely sure that the ray direction modification would completely prevent the train from climbing one of the rails (although it would be less probable). Anyway it's worth trying (and after all real trains can get out of the tracks in some cases...). If you make any progress keep us updated :D

User avatar
Yann
Posts: 52
Joined: Wed Sep 28, 2011 8:36 am
Location: France
Contact:

Re: Modeling a train using joints & a number of raycast vehi

Post by Yann » Tue Nov 29, 2011 3:51 pm

Maybe I'm out of subject, but isn't it a problem that should better be solved by using a prismatic joint ensuring the train will stay on the rails ?

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by Flix » Fri Dec 02, 2011 12:45 pm

Yann wrote:Maybe I'm out of subject, but isn't it a problem that should better be solved by using a prismatic joint ensuring the train will stay on the rails ?
Probably... Actually I haven't tried all the constraints in the Bullet engine yet... With "prismatic joint" do you mean btSliderConstraints (or maybe a generic 6 dof with two translational axis locked) ? Well, in that case, can that constraint work for curved tracks too ?

User avatar
Yann
Posts: 52
Joined: Wed Sep 28, 2011 8:36 am
Location: France
Contact:

Re: Modeling a train using joints & a number of raycast vehi

Post by Yann » Fri Dec 02, 2011 12:54 pm

Yeah, that's the one I meant ;)
You're right about the fact it won't work on a curved trajectory...

I'm still not sure building the rails with some hack to ensure the train wil stay on it is the best solution either. What about building your own 'trajectory constraint', that would ensure the train to stay on it's rails ? I know the guys from Haption made it in their own physic engine for their haptic device...

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by Flix » Sat Dec 03, 2011 12:12 pm

Yann wrote:What about building your own 'trajectory constraint', that would ensure the train to stay on it's rails ? I know the guys from Haption made it in their own physic engine for their haptic device...
Well, I've never tried creating new constraints so far, so I think I'll keep using rigid body wheels for it (although maybe somebody could try changing the btSliderConstraint orientation reference every frame and see if it works...). Anyway for straight trajectory that's definitely the way to go :)

dumbo2007
Posts: 66
Joined: Sun Jan 30, 2011 8:46 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by dumbo2007 » Fri Apr 27, 2012 3:27 am

Well I have got a basic train implementation running :

http://www.youtube.com/watch?v=L1LoaMKa ... e=youtu.be


It has got curves,line change junctions, signals, a basic cab controller, engine speed variation and brakes and I am working on going up hills.


So I basically ditched the raycast vehicles linked together thing, because inpsite of the fact that I was able to control the wheels of the cars using a simple controller to turn them around sharp turns, there was an issues with linking multiple cars together. The point-to-point connectors in Bullet are just too springy for real railroad car weights. But its easy to make a set of light cars follow a trajectory, maybe a toy train example later :P

I used the railway car dimensions from my country : http://in.answers.yahoo.com/question/in ... 537AAtXY0C

For more realistic railroad modelling I decided to take a cube and make it float above a point on a plane first. For this I simply use a spring force with the force increasing linearly as the cube gets closer to the surface. So this is good enough to make it float. Since the railroad car has also got to be perfectly parallel to the underlying track, I actually apply 2 "up" forces, one each at the center of the axle of the front and rear wheels.

Then there is the rotation of the cars around the y-axis(the "UP" axis). The cars again remain parallel and facing forward through the use of a counter torque, if the car "swings" away from the tracks. This is based on the angle of deviation from the current direction of the track, with a torque applied to make this angle 0 always. It was easy to get this angle for straight segments of tracks. For curved segments, the radial vector connecting the center of the curve(curved tracks are always circular) with the center of the car is useful.

I experimented with tracks which gradually curve from 0 curvature(straight segment) to maximum curvature to get around sharp bends. The gradual curvature of track is whats used in real railroad tracks to prevent sudden changes in directions at high speed which would cause the cars to bang hard against the rails. However its not easy to get the radial vector at any point along a curve which is not circular. So I thought a gradual bending curve can also be approximated by a series of circular segments whose normals match at the ends. Thats the plan as of now. An automatic track layer later perhaps :)

The cars are kept on the track using a simple spring force too of the form kx where x is the deviation of the center point of the axle for the front (and rear) wheels. Its easy to get the nearest point on a straight track , to the center point of the axles, and then apply a force towards the track to keep the cars on the rail trajectory. For curving tracks its even easier as a fixed radial distance has to be maintained from the center.

This is the trajectory constraint discussed earlier. I really hardened the spring constants using values like 100000 to ensure the force was really great in case any of the cars deviated from the rails.

Now for the engine, the basic engine force is just a force applied on the center of the front most car through applyCentralForce(). The toughest part to fix which took me over 3 months was the link forces between the cars which pull the car or slow them down.

The link forces are applied based on the distance between 2 cars. Every car apart from the engine, applies a force on itself proportional to its distance from the car ahead of it. This force is also applied on the car ahead as a pull, so the engine has to really pull hard to get a long line of cars moving.

The last thing to add was rail infrastructure in the form of junctions. That took over a week to fix too. basically the train is diverted to a circular section thats touching a straight segment, when the junction is active but there are multiple cases. For example the junction must also work for cars rolling backwards etc, overlapping active junctions must not confuse the cars !!

With that out of the way, I added signals and a basic cab controller. The controller looks at the signal ahead of it on the same track and reaches a certain speed setpoint based on yellow or green. It applies brakes on red which is applied on all the cars.

The system is stable at high speeds and for curves there are speed limits. Sharp curves taken at high speeds cause the link force to ramp up suddenly to unmanageable levels so currently I have clamped the maximum link forces, that helped.

Thats about it, thanks for reading :)

dumbo2007
Posts: 66
Joined: Sun Jan 30, 2011 8:46 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by dumbo2007 » Sun Apr 29, 2012 7:47 pm

So the code for this train sample is right here :

http://code.google.com/p/surface-physic ... ig%2FTrain

I am adding more comments to the code gradually.

/*
* This sample application uses Bullet Physics to model a toy train running around a track.
* It features signals, a cab controller and rail junctions too !
*
* The basic idea is to make a rigid body follow a given trajectory. The simplest trajectory is a
* straight line. To make a closed path, one can use a circle or one can join together straight lines
* & circular curves. So as the rigid body moves along the path, its "handed off" to the next curve at
* just the right moment when it comes to the end of the current curve.
*
* This idea can be extended to a simple trajectory constraint which contains a list of curves.
*
*
* In the context of this sample, the curves are called Track segments. They can be straight or circular
* curves. A railway track is simply a list of track segments. The track is only drawn but not modeled
* as a rigid body.
*
* The railroad car is a cuboidal rigid body. These are the only rigid bodies in this sample. All the rest
* are collision shapes.
*
* No other features of Bullet like constraints, are used. All forces are calculated and applied directly on the
* cars. The front most is car 0 and is the engine.
*
*/


There are 2 tracks, the trains can be directly accelerated/decelerated by using the proper keys or you can just set the signals and let the cab controller take over. Try to make the engine stop inside the length of one of the stations :)


Basic Engine Keys :
UP/DOWN arrow for accelerate/brake
F9 to reverse engine force

Signal control:
Its 2 step process, first you choose the signal then you set the lamp. The currently selected signal will have a red box hovering above it.
Move left or right among the signals in a single track using LEFT/RIGHT arrow
To goto track 2 signals press F8, to come back to track 1 F7
Page UP/Page Down : for setting the signal lamps of the currently selected signal

Camera control :
F10 : camera focuses on train : toggle on/off


Junction control:
F11/F12 : to activate track1 to track 2 junction and track2 to track 1 junction respectively.
Red means junction is inactive, grey means active


Have fun railroading !

STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Modeling a train using joints & a number of raycast vehi

Post by STTrife » Sat May 05, 2012 6:09 pm

Impressive, thanks for posting it!

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by Flix » Sun May 06, 2012 8:27 am

Just to say thank you to dumbo2007: it seems that you want to simulate a complete train system! :D

dumbo2007
Posts: 66
Joined: Sun Jan 30, 2011 8:46 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by dumbo2007 » Sun May 06, 2012 4:39 pm

Yeah I have plans for adding multiple rakes and simulating steam engines (that would be awesome :) )


So I was wondering if as a offshoot , a new trajectory constraint would be useful in Bullet. It would basically hold a linked list of directions and their lengths i.e. straight and circular segments. It could then make any point on a rigid body stay on the trajectory by applying a force towards it whenever the body swayed away :



Image


So for example the trajectory can be composed of the above connected segments (1 straight, 3 circular). The goal is to keep the center of the cube on the trajectory. At (1) everything is fine. Now the user tries to move the cube forward using some force and by the time the cube reaches say (2) it has moved away from the defined trajectory so a force is applied on it to bring it back "on line" :P

Also a torque can be applied to keep the cube's edges parallel to the normal at that point on the curving trajectory as in (3)

Sounds useful ?

STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Modeling a train using joints & a number of raycast vehi

Post by STTrife » Sun May 06, 2012 8:52 pm

Perhaps just apply a force towards the center of the curve (every curve could be seen as part of a circle) on every wheel-pair on the train? Then you don't need to explicitly give an angular force or anything, makes the simulation more based on reality :)

dumbo2007
Posts: 66
Joined: Sun Jan 30, 2011 8:46 pm

Re: Modeling a train using joints & a number of raycast vehi

Post by dumbo2007 » Mon May 07, 2012 3:13 am

Yep , thats exactly what I do !

Post Reply