Rotation problem

This topic is 2337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
i dont understand your problem :/

Share on other sites
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)

Share on other sites
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 } } `

Share on other sites
Sorry I did not explane clearly. Thanks for all your reply's. But what does the abs function do?

Share on other sites

... But what does the abs function do?

It means the absolute value of its argument.

Share on other sites
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

Share on other sites
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.

Share on other sites
I would not know why, but I do indeed use opengl for rendering

Share on other sites
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).

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5

• 13
• 14
• 66
• 14
• 15
• Forum Statistics

• Total Topics
632130
• Total Posts
3004288

×