Set Body.Rotation manually

Topics: Developer Forum, User Forum
Mar 14, 2009 at 6:15 PM
Hi! I wanna know if it is ok to set the Body.Rotation manually or it can mess with any internal calculation, collisions or something.
My project is pretty much like a cannon that must always point to the direction where the mouse cursor is, to achive that i'm using:

        private void HandleMouseInput()
        {
            MouseState state = Mouse.GetState();

            aimVector = new Vector2(state.X, state.Y);

            shooter.Body.Rotation = (float)Math.Atan2(aimVector.Y - shooter.Body.Position.Y, aimVector.X - shooter.Body.Position.X);
        }

Just to make sure, is the engine ok with that?
Thanks!

BTW I found Farseer Physics two days ago and i think it awesome and very simple to use! Keep up the good work!
Mar 14, 2009 at 9:38 PM
Well im pretty sure that it screws up like half a dozen variables inside a body, so what you would want to do is a little more complicated: use an AngleLimit joint and set the min and max angles to the angle that points toward the mouse.
Mar 14, 2009 at 11:27 PM
Ok! Thanks for your reply, I'll give it a try!
Mar 15, 2009 at 12:42 AM
Well, I tryed FixedAngleJoint and FixedAngleLimitJoint, both worked except for one thing. When the angle is about to receive the value of "Pi" the body do a 360º turn to the wrong side instead of just rotate a bit to the right side.
That is, if i'm moving the mouse slowly to the left the body is slowly rotating left, when the angle get closer to Pi the body rotate right 360º instead of keep rotating left.

Totally look like a bug, I found something similar in that old thread with no responses.

Any solutions to this? I'll try the source code to "fix" it while no answer appear..
Thanks!
Mar 15, 2009 at 1:08 AM
well what youll see in some games is that you cant loop directly up, it will snap to the next position, you could do something similar and giver it the angle limit close to 360 but not quite, it will only be a little jump hardly noticeable i assume.
Mar 15, 2009 at 2:51 PM
Edited Mar 15, 2009 at 2:53 PM
@Tlegion: You're right, but thats not wroking either, i tryed with joints and springs and both have the same problem.
Let's assume the problem occur when passing 180º (i'll use degrees to make it easier to explain), if i change the FixedAngleJoint.TargetAngle from 179 to 181, the body will spin to the wrong side. If I change from 170 to 190, same problem... So the only way to make it "jump" is setting Body.Rotation manually wich brings me back to te first problem. I'll use only Body.Rotation and see if the rest of the simulation works ok, i can't see other way to correct this.

Should I submit a bug report or open a new discussion to talk about that joint/spring issue?

Thanks!
Mar 15, 2009 at 4:11 PM
well thats confusing, im not quite sure whats actually happening on screen with your code, can you only go 180 degrees?
and i think what may be the problem is that its applying to torque in an attempt to reach that target angle.
If you set max torque to zero what happens?
sorry i cant help more but this doesnt make much sense to me
Mar 15, 2009 at 5:33 PM
I've created a small project to show the problem, you can download it here

Running the sample you can see:
The yellow part of the rectangle body must always point to the mouse cursor. When you move the mouse to the left and the TargetAngle comes closer to +/-Pi, instead of keep turning to the way you're moving the mouse the body turns the other way 360 degrees to match the terget angle.
Also you can notice that Atan2 return negative angles and Body.Rotation always return positive angles. Yesterday I did the needed math to make Atan2 return the correspondent positive values but the only change was that the problem "flipped", instead of happening at "Pi angle" position it happens at "zero angle" position.

Finally, on top of "Game1.cs" I added a #define statement (commented by default) to switch between using FixedAngleJoint.TargetAngle and Body.Rotation, just to show that Body.Rotation one seems to work fine.

Sorry for all the trouble, but with the project i think you guys will be able see what i mean and, hopefully, point a solution!

Thanks!
Mar 15, 2009 at 11:38 PM
well in my opinion its find to set the body.rotation,
but of course the other people here ar emuch smarter than i am and know the engine better
but ive done it and had no problems sooo
COUGH*do it*COUGH
Mar 15, 2009 at 11:46 PM
hahahaha... Body.Rotation will be, i can't waste more time with that. But i'll probably come back to that in the future
Thanks!
Mar 16, 2009 at 10:15 PM
Well i could be wrong that it screws stuff up because i was doing it at one point and it was fine.