Reputation: 3
I followed a Youtube video guide to make an Asteroids game with LibGdx, the person who made the video included no touchscreen controls as they were just doing it for the desktop version. I have managed to use the LibGdx touchpad and I have got it almost working but with a few problems when rotating the ship.
My Touchpad gives values of between - pi and + pi depending on the angle (Right hand side is 0, bottom is the negative values, top is positive). The same goes for the direction the ship is currently facing.
The problem is that as the ship slowly rotates if your on the left side and change from -pi to +pi with the touchpad the ship will rotate around the long way. Every solution to this that I have come up with has caused a problem else where. My latest solution below I thought worked but has the same problem just in a different area.
radians is direction ship is currently facing. touchPadDirection is direction that the touchpad is held. Both in radians.
if (radians < touchPadDirection ){
if( radians - touchPadDirection < MathUtils.PI && touchPadDirection > MathUtils.PI /2 && radians < MathUtils.PI /2 ) {
System.out.println("1st choice");
setRight(true);
setLeft(false);
}else{
System.out.println("3rd choice");
setLeft(true);
setRight(false);
}
}
if (radians > touchPadDirection) {
if( radians - touchPadDirection > MathUtils.PI && touchPadDirection < MathUtils.PI /2 && radians > MathUtils.PI /2) {
System.out.println("2nd choice");
setRight(false);
setLeft(true);
}else{
System.out.println("4th choice");
setRight(true);
setLeft(false);
}
}
}
The only rotation problems I have found with google seem to help if the item instantly rotates as it will just face the direction of the ship. So I need to somehow get it keep rotating the correct way at all times (always rotate in whichever direction will be fastest). Remember the values are between -3.14 and 3.14 and the original problem comes when the small change causes the ship to rotate the long way around.
I'm not the best at explaining so hopefully someone understands what I am trying to achieve and thanks in advance.
Upvotes: 0
Views: 175
Reputation: 3
I think I worked it out, tested this a lot and everything seems to be working correctly.
public void rotate(float touchPadDirection) {
if (radians > 0 && touchPadDirection < radians - MathUtils.PI){
setLeft(true);
setRight(false);
}else if (radians < 0 && touchPadDirection > radians + MathUtils.PI){
setLeft(false);
setRight(true);
}else if (touchPadDirection > radians){
setLeft(true);
setRight(false);
}else if (touchPadDirection < radians){
setLeft(false);
setRight(true);
}
}
Like everything the solution is always simpler than most of the things I try, As before radians is the current facing of player ship and touchPadDirection is the current direction it is held in. If anyone has an even simpler solution I would still like to know as I will likely use something very similar in the future.
Upvotes: 0