1
\$\begingroup\$

I'm implementing vehicle movement in a 2D environment, where a vehicle consists of a front and a back "axle", both represented as 2D points. When the front axle moves, the back axle should follow in that direction, mimicking the way vehicles move. The way I have it right now looks like this:

Sharp right

After moving the front point A (white), I place B on the line between A and B such that a constant distance (the vehicle length) is kept. More accurately, I use the vector from A to B, normalize it, multiply it with the vehicle length, and add it to A, to get the new B. When searching for similar issues, I found this question's answer to come to the same technique.

As you can see, the resulting movement is quite recognizable as vehicle movement. The front also does not need to move as smoothly. Below I moved the front manually by dragging it around. As you can see, backwards movement also works perfectly:

Free movement

I want to use this movement system as the back bone of a puzzle game, so its stiffness is actually an asset, since it is also quite predictable.

The only issue that I have is that it takes a good while for the vehicle to straighten out, by which I mean have the back come in line with the travel direction of the front. I'm looking for a way to control how quick this happens.

I have already played around with biasing/weighing the back point onto the line on which the front appears to be traveling, but that resulted in weird moving behavior for any other scenario than moving in a perfectly straight line. I also haven't been able to think of a different system that would inherently straighten out quicker.

So... I'm happy to try out any ideas that you might have!

Thanks!

Update 1: I've implemented the idea from @GaleRazorwind. This results in the back point moving away from the front point: Biasing point towards A1

\$\endgroup\$
3
  • \$\begingroup\$ In the initial image, you have the front wheels effectively rotated 90 degrees from the center, which is a very unnatural movement for a car. I think this is causing the vehicle to not straighten out as fast. \$\endgroup\$ Jan 12, 2021 at 23:48
  • 1
    \$\begingroup\$ A trick I've used for this for camera control hinges (pun intended) on the observation that the shorter the wheelbase between the two axles, the more quickly the trailing axle swings behind the leader. So what I've done is to decouple the length of the wheelbase used to control the swinging from the actual distance between the axles. I effectively track a virtual rear axle, closer to the lead. I update this virtual axle's position as you describe. Then I extend the line from the lead axle through this virtual real axle, to the length of the desired wheelbase, and place the real rear axle there \$\endgroup\$
    – DMGregory
    Jan 13, 2021 at 0:57
  • \$\begingroup\$ @DMGregory Simple but effective idea, thanks! \$\endgroup\$
    – Rutger
    Jan 13, 2021 at 8:08

1 Answer 1

1
\$\begingroup\$

Ok, I put in a bit of thought on how to increase the speed of straightening out. Let's start with a basic image showing what you have right now:

ProblemImage

The car starts with its front at A1 and its rear at B1. During the update, the front moves to A2, so you calculate the normal from A2 to B1 and use that to find B2.

Now, you are trying to get the vector from A2 to B2 to line up with the vector from A2 to A1 more quickly. So, take a vector from B2 to A1, normalize it, and then multiply it by a scaling value ("artist defined"). You will then offset B2 by this new scaled vector to get B3.

ProblemImage2

However, now we are no longer at the proper distance from A2, so create a vector from A2 to B3, normalize it, scale it by the vehicle length, and add it to A2 to get B4, which is the final position.

enter image description here

Notes: This will cause issues as you approach moving in a straight line, so you can check the Dot Product between A2A1 and A2B2 before performing these extra steps. If the dot product is close to 1/-1, don't do the extra steps. You could also multiply your scaling factor by the inverse of the dot product (1 - Dot). This will make your scaling more aggressive at sharper angles and less aggressive at shallow angles.

\$\endgroup\$
5
  • \$\begingroup\$ I haven't implemented it yet, but I think in most cases B4 will not lie on A1B1 as you have drawn, but it would rather lie further away. This would result in the back actively moving/shifting away from A2, which results in unnatural movement. The biasing I have tried previously was projecting B1 onto A1A2, and then moving that point towards B1 such that the length was kept. Either way, I could be wrong, so I will implement it and update my question with your idea! \$\endgroup\$
    – Rutger
    Jan 13, 2021 at 8:16
  • \$\begingroup\$ Apologies, my drawing is not to scale and the points are all in arbitrary positions. My algorithm should still maintain the vehicle length from A2 while bringing the vectors A2A1 and A2B2(4) together faster. \$\endgroup\$ Jan 13, 2021 at 14:08
  • \$\begingroup\$ Also, try reducing your scaling factor. As I mentioned, you are moving your front axle at a 90 degree angle in the image. Cars have a minum turning radius and if you try to make it turn too sharply, the back end will swing out. This would be like a loss of traction on the rear wheels. Try making it so your front axel has a forward direction. Holding down W makes it move forward, S makes it move backward. A and D make it start turning to the left or right while moving forward. Then set a degrees per second value to create the minimum radius. \$\endgroup\$ Jan 13, 2021 at 14:16
  • 1
    \$\begingroup\$ I implemented your proposed approach soon after my previous comment. With one alteration, being that to calculate the distance from B2 to B3, I do not normalize the vector from B2 to A1 before multiplying the scale factor. The resulting behavior is visualized in the last GIF of the question, where you can see that the back point moves sideways as predicted. The vehicle does straightens out faster, but if I reduce the scaling factor to reduce the swinging, the straightening out becomes slower as wel... \$\endgroup\$
    – Rutger
    Jan 13, 2021 at 14:56
  • \$\begingroup\$ Regarding your point on the sharp 90 degree angle: I chose such an angle deliberately. Think of the vehicle as a trailer that has its connection point being moved around by a truck, which is slowly and carefully maneuvering around a tight curve. \$\endgroup\$
    – Rutger
    Jan 13, 2021 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .