7
\$\begingroup\$

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 ?

\$\endgroup\$
3
1
\$\begingroup\$

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...

enter image description here

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...

enter image description here

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...

enter image description here

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

enter image description here

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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 ? \$\endgroup\$ – Bloke Apr 17 '16 at 4:46
  • \$\begingroup\$ 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. \$\endgroup\$ – Bloke Apr 17 '16 at 4:49
  • \$\begingroup\$ 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 ? \$\endgroup\$ – Bloke Apr 17 '16 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.