Rotation problem

Started by
8 comments, last by alvaro 12 years ago
Hello, I am flokkienathur and I'm working on a top down shooter.
There is a player who's rotation get adapted by the mouse (the player watches the mouse). But I don't the player to be able to move realy fast.
So I tryed to do it like this :


if(rot > prevRotation+10){
rot = prevRotation+10;
}if(rot < prevRotation-10){
rot = prevRotation-10;
}

but if you do this, if the player reaches the top, he turn al the way alround instead of just over the 0 degrees.

Like in the image (Where first is is the first position and where second is the second position.).

[attachment=7843:sircle.bmp]

So... does anyone know the best way to fix this?

Please help :)
Advertisement
i dont understand your problem :/
"There will be major features. none to be thought of yet"
The easy way:
assuming rot is the new rotation target and prevRotation is the current orientation and that your rotation is in degrees. (most math libraries use radians but since you're adding 10 its probably degrees you're using)

diff = prevRotation - rot;

if (abs(diff) >= 180) {
//we are trying to turn the long way around so lets swap it around
diff = 360-diff; //instead of going 300 degrees in one way we will now go 60 degrees the other way
if (diff>360) diff-=360; //if diff was negative we'd end up with a value above 360 which we don't want
}

if (abs(diff)>10) {
//trying to rotate too fast so we divide diff with absdiff (to get 1 or -1 depending on the sign of diff) and multiply by the max rotation to get -10 or +10
diff = 10 * diff/abs(diff);
}

rot = prevRotation+diff; //and now we should be done.


I didn't test the code so there might be errors, you should get the basic idea. (There are optimizations you can do aswell but i think clarity is more important here)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
You can use a unit-length complex number to indicate a direction in 2D. This just means that instead of storing the angle alpha, we'll store (cos(alpha), sin(alpha)). Complex multiplication is now addition of angles, and complex division is subtraction. This might seem unnecessarily complicated if you are not very familiar with complex numbers, but the resulting code ends up being very clean and there are basically no especial cases to think about.


#include <iostream>
#include <complex>
#include <cmath>

typedef std::complex<double> C;

double sign(double x) {
return (x>0.0) ? +1.0 : -1.0;
}

C rotate_towards_target_with_cap(C current, C target, double max_angle) {
C adjustment = target / current;
if (adjustment.real() < std::cos(max_angle)) {
adjustment = C(std::cos(max_angle),
std::sin(max_angle) * sign(adjustment.imag()));
}
return current * adjustment;
}

int main() {
double const degrees = std::atan(1.0)/45.0;
C current = std::exp(C(0.0, 40.0 * degrees));
C target = std::exp(C(0.0, -50.0 * degrees));

for (int i=0; i<=10; ++i) {
std::cout << target << ' ' << current << '\n';

current = rotate_towards_target_with_cap(current, target, 10.0 * degrees);
current /= abs(current); // Normalize so the length of `current' doesn't drift over time
}
}
Sorry I did not explane clearly. Thanks for all your reply's. But what does the abs function do?

... But what does the abs function do?

It means the absolute value of its argument.
Thanks! It works now, it was much easier than I excpected! Btw, i use degrees because the graphics rotation also works in degrees and secondly i find the numbers easier to work with
Things generally work much better if you use radians for internal computations. I know that some libraries expect degrees (in particular OpenGL), but it's hard to understand why: I am sure the first thing they do in the implementation is convert your angle to radians.
I would not know why, but I do indeed use opengl for rendering
Still, I would do everything in radians for my own sanity and then convert to degrees when talking to OpenGL. If I were using angles, that is. As I said earlier, I'd much rather use unit-length complex numbers instead. If you ever need 3D rotations, using quaternions feels a lot like using complex numbers in this case (with several complications).

This topic is closed to new replies.

Advertisement