# Bike game - following a road path

I am writing a game in java which has a bike riding down a road. I intend to add some obstacles on the road which are structures having a slope, which the biker must negotiate. In other words, the bike must climb and run along the slope whenever it encounters one so obviously it must be aligned accurately with the slopy path. Im currently doing this using trig functions to find the angle of rotation, x y coordinates for the bike when it is climbing, descending etc. Can someone suggest a better algorithm to achieve this please ?

• Is this top-down or side-view?
– Anko
Apr 16, 2016 at 23:22
• Thanks Anko - the game has a permanent straight side view Apr 17, 2016 at 4:26
• Mar 8, 2017 at 2:14

I don't' think you can avoid the trig really, but what you can do is improve just how you calculate the angle.

Your question mentions the "x y coordinates for the bike", but in fact the bike doesn't have one coordinate. It has two coordinates - the two wheels.

Specifically, don't base it on the bike's location and a single point. If you use just a single fixed point (like at the bike middle), it's not going to work when the bike first crosses into a slope - the bike's front wheel won't tip up until the mid point crosses onto the slope.

Here's a picture of what it will look like if you calculate from the middle and just enter a slope...

Instead, calculate the rotation based on where the front and back wheels touch the ground. Calculate Y at back wheel, then Y at front wheel, then what the angle is between the two points. Then you can make it work with uneven ground, and the motion will be a lot more believable, and will look like this...

This also will let you handle smaller bumps nicely. Based on wheel to wheel slope, you'll actually have your bike front wheel go over the bump, come down the other side, then have the rear wheel go over it. Calculating based on one point (middle of bike), you'll get this...

But calculating based on the bike wheels, you'll get this...

That's more or less how I would do it too. One thing you can try is to have the ground pieces contain a normal vector that you can quickly query to align the bike to the slope.

Vector normalization is mostly used when you want the vector to have a length of 1. Another use is if you want the vector to have a specific length then you would normalize the vector which guarantees the vector has a length of 1 and then multiply by the number you want it's length to be.

• Thanks - I am actually new to programming that involves trig calculations so im struggling with the transformations. but after some practice and following various web tutorials, ive managed to write my own functions in my code for rotation and speed manipulations. There is one thing that still confuses me. Ive read a lot about the term vector normalisation and I get the concept but can u show me how it can be used in code ? Apr 17, 2016 at 4:46
• for eg. in another game im making, Ive got a helicopter that flies around and shoots bullets on user key press. Now there could be some enemy objects on screen when the bullet is fired. Obviously the bullet wont be aimed at any particular enemy coz the bullet must follow the position and direction of the helicopter when fired. But if the bullet collides with an enemy on its way, then both the bullet and enemy disappear and user scores a point. Apr 17, 2016 at 4:49
• Now im doing all this simply by checking if the enemy coordinates collide with the bullet. So it would be roughly something like ... if bullet.X == enemy.X AND bullet.Y == enemy.Y. Obviously i also take their sizes into account. This allows me to achieve the result i want but i do not use vectors here. Can the concept of vector normalisation be used in this context for a more optimised or cleaner code ? Apr 17, 2016 at 4:49