- Home /
Accelerometer car steering and screen tilt control function
Hello,
I'm searching for a more convenient way to handle the steering control and horizon screen tilt.
At the moment I'm using this function as the base for my car steering:
public static float RotationYXAngle180Degree(Vector3 currentAcc) { float rotationAngle = Mathf.Rad2Deg * Mathf.Atan2(currentAcc.y, currentAcc.x);
return rotationAngle;
}
For the screen horizon tilt I'm using as the base this function:
public static float RotationYXAngle360Degree(Vector3 currentAcc) { float rotationAngle = Mathf.Rad2Deg * Mathf.Atan2(currentAcc.y, currentAcc.x);
if (rotationAngle < 0f)
rotationAngle = rotationAngle + 360.0f;
return rotationAngle;
}
The Vectors are calibrated with an 4x4 Matrix. Here arises already a part of my problem. If I play the game lying on my back then the screen is tilt 180 degree in the wrong direction and the car will steer the wrong way. I tried before to use only the accelerometers Y axis, It just didn't respond well.
A new solution or direction to my problem is greatly welcome ;-).
Peter.
I found my own answer ;-) I have changed the the following line of code in both functions: float rotationAngle = $$anonymous$$athf.Rad2Deg $$anonymous$$athf.Atan2(currentAcc.y, currentAcc.x); to: float rotationAngle = $$anonymous$$athf.Rad2Deg $$anonymous$$athf.Atan2(currentAcc.y, $$anonymous$$athf.Abs(currentAcc.x)); The $$anonymous$$athf.Abs function makes the $$anonymous$$atrix unnecessary.
Perhaps put it as an answer and mark it as accepted, rather than a comment. It'll help others find the solution easier when searching ;-)
Answer by morothar · Feb 08, 2010 at 01:48 PM
Since this question still at the top o the unanswered questions I hereby answer it... Hopefully it will be marked as answered soon....
I found my own answer ;-) I have changed the the following line of code in both functions: float rotationAngle = Mathf.Rad2Deg Mathf.Atan2(currentAcc.y, currentAcc.x); to: float rotationAngle = Mathf.Rad2Deg Mathf.Atan2(currentAcc.y, Mathf.Abs(currentAcc.x)); The Mathf.Abs function makes the Matrix unnecessary. TriplePAF Jan 9 at 0:19
Answer by CallToAdventure · Nov 04, 2011 at 08:38 PM
Changed this line in both functions:
float rotationAngle = Mathf.Rad2Deg * Mathf.Atan2(currentAcc.y, currentAcc.x);
with
float rotationAngle = Mathf.Rad2Deg * Mathf.Atan2(currentAcc.y, Mathf.Abs(currentAcc.x));
Your answer
Follow this Question
Related Questions
Car Movment script not fully working 0 Answers
CAR ROTATING HELP 2 Answers
Reduce steerangle at high speeds 1 Answer
hands rotating with steering wheel 1 Answer