Train Car Problems

BlitzMax Forums/BlitzMax Programming/Train Car Problems

Ryan Burnside(Posted 2006) [#1]
I would like to make a train in my game that the player can controll. It is not like a normal train that rides on rails, thie would be simular to a child's pull train that just rolls along the ground. My problem is making and controlling the cars they need to stop when their corners touch and also they need to maintain a set distance from each other at all times. It would be simular to a chain except the links are always a set distance apart.

Here are the specs:
Each car measures 16x24 pixels. The space between the ends of the cars is 4 pixels. Cars need to never intersect, even when the train changes direction. If you can maybe help me I would be really gratefull. I doubt that I can solve this on my own. I do have a function to draw the rotated rectangles, you only need to help me find the midpoints of each car....
Here is a pic to help with the concept:



Will(Posted 2006) [#2]
Tips:

1) keep track of each traincars position from the center of the front of the traincar, not the center of the traincar.

2) work your way down the traincars from the first one, repositioning each, and then fixing it if their corners intersect

3) the corner intersection can be checked by comparing the rotation of two traincars, rather than using any collision detection.


Byteemoz(Posted 2006) [#3]
This is a "chain of dots" approach:
NodeDist controls the length of a car (including the distance between the cars) and NodeDist2 is the minimal distance between the startpoint of one car and the endpoint of the next car (indirectly the minimal angle between two cars).

-- Byteemoz

Edit: Changed the example to use float instead of integer math.



Ryan Burnside(Posted 2006) [#4]
Wow that's amazing stuff! Thanks.


Smurftra(Posted 2006) [#5]
Have a question:

Why do you loop it 4 times? (the n loop).

Is it for precision, if i looped 20 times would it be more precise?

also, you can put the lines with the angle calculation in the following IF because you dont need the angle unless the IF conditions are met


Ryan Burnside(Posted 2006) [#6]
Yep I get it. How did you learn this or did you just invent the solution here?


Byteemoz(Posted 2006) [#7]
Why do you loop it 4 times? (the n loop).
Is it for precision, if i looped 20 times would it be more precise?
In every loop we gain precision by correnting the distance and angle between the dots and we lose precision by using computer math. So after a certain point (I'm not sure how many loops exactly) we will actually lose precision...

you can put the lines with the angle calculation in the following IF because you dont need the angle unless the IF conditions are met
I hacked this together in a few minutes - it's 100% unoptimised. ;-)

Yep I get it. How did you learn this or did you just invent the solution here?
It's an algorithm to model strings in physical simulations. I think I got it from here...
-- Byteemoz

PS: I changed the example to use float instead of integer math.