LibGDX - Car (Sprite) follows (or moves on) the way (path) and auto rotate?

Hi,

We have been trying since 6 days to solve this problem. :frowning:

On the image you can see a car and a way. We want, that the car (sprite) follows (or moves on) this way (path) with autoRotate and constant speed.

We made this “way” with inkscape. We can save the “way” as .svg to get the path.
The car should follow the path and changing his direction with constant speed.

http://img43.imageshack.us/img43/9229/text4621.png

for example the .svg file:

...
<path
   style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
   d="m 196.47467,414.45085 ... etc."
   id="path2985"
   inkscape:connector-curvature="0" />
...

The SVG path file is not important. We can also write our own path array.

We thought, that Path moving is so important for game developers. And we never thought, that this takes 6 days
at the beginning.

We can’t find any good examples or tutorials.

Yes, it’s true. We asked this question in another forum. But after 6 days, we need now a solution.
This is the forum where we asked the question:
http://badlogicgames.com/forum/viewtopic.php?f=11&t=7510&sid=de880d0aeef7c7237ea3e1e496cd2842

We heard about tween engine by greensocket. There is a animation on the website. It’s called “bezier example” and it’s in the
TweenLite/Max Plugin Explorer.
http://www.greensock.com/v12/
We first thought. Great, but it’s only for Flash and Javascript. We first thought to the change the JS code to java, but the
code is too much.

We are grateful for any help.

Thanks…

If your track isn’t overly complex, I suppose you could just set some “points” on the track on corners and whatnot, and then rotate the car vector to each point, and then accelerate. As the car passes that point, you could just have it vector rotate to the next point, and so on.

Try tris code https://github.com/fr1zle/OpenMali/blob/master/openmali/src/main/java/org/openmali/curves/CubicBezierSpline.java

  • Construct a Spline with your path points (and optional control points)
  • use samplePoint() to get two points from the spline a little bit before and a little bit after the desired car position
  • create a vector pointing from one point to the other
  • normalize it -> now you have a direction vector to use for your car rotation

The code is for 3D, so just use 0 as z component

:o

That’s some crazy ass code just for a cubic Bezier Curve…

well… thanks :stuck_out_tongue:

@cylab: Ok. We will look at this code.

EDIT: Are there some examples of how to use this code? And can we make a one round track like the first
picture above?

I did something like this just last week, even though it wasn´t cars. I set up a path of nodes for my game-enemy. Each node has a radius and when the enemy is within that radius he switches to the next node. This made him walk around in a perfect rectangle. Next thing I did was getting rid of the instant change in direction by implementing a seek behavior. This makes the turns smoother and more “realistic” looking. I think this would work out for you too: Set up a bunch of node in each turn. Implement a seek behavior that´s “seeking” for the next node/turn and then you should have a car that´s following a smooth path.

Here´s a link if you want to read about seek behavior (not java, but the concept is there): http://rocketmandevelopment.com/2010/06/11/steering-behaviors-seeking/

ps. It should be noted that my enemies did not rotate, but that shouldn´t be to hard to work out…

No, but just try what I described in my last post

Look up Cubic Bezier Splines - they are not closed paths. For a round track you have to set the first and the last base point to the same coordinates and set the control points to mirror at the basepoint.

You should be able to export a cubic b-spline from inkscape and use exported coordinates as base- and control-points. But since I never used inkscape, you have to google or ask in their forum.