
Advertisement
Tom 'Rossy' Rosbotham
Member
Content Count
8 
Joined

Last visited
Community Reputation
100 NeutralAbout Tom 'Rossy' Rosbotham

Rank
Newbie

How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
Adding wrapping to Simon's code create the functionality I was looking for, but thank you everyone else (particularly you, Zael, as you've taught me some syntax techniques). As you said Zael, Simon's solution was very similar to yours... I don't actually understand how his works and yours didn't. O_o public void mouse() { MouseState mouseState = Mouse.GetState(); Vector2 mouseLoc = new Vector2(mouseState.X, mouseState.Y); if (mouseState.LeftButton == ButtonState.Pressed) { if (!position.Equals(mouseState)) { Vector2 direction = position  mouseLoc; float destAngle = (float)(Math.Atan2(direction.Y, direction.X)); position.X += speed * (float)(Math.Cos(rotation)); position.Y += speed * (float)(Math.Sin(rotation)); Console.WriteLine(rotation + " + " + destAngle); float diff = destAngle  rotation; if (diff > Math.PI) { destAngle = (float)(Math.PI * 2); } else if (diff < Math.PI) { destAngle += (float)(Math.PI * 2); } if (Math.Abs(destAngle  rotation) <= movingRotationSpeed) { rotation = destAngle; } else { if (destAngle  rotation <= 0) { rotation = movingRotationSpeed; } else { rotation += movingRotationSpeed; } } rotation = WrapAngle(rotation); } } } private float WrapAngle(float theta) { while (theta < MathHelper.Pi) { theta += MathHelper.TwoPi; } while (theta > MathHelper.Pi) { theta = MathHelper.TwoPi; } return theta; } 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
Using an ifstatement had the same issue as my original code. I couldn't draw a complete circle on the screen with the ship, as the moment the destination angle went from negative PI to positive PI it would attempt to turn in the opposite direction (the westaxis). 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
Try this: float diff = destAngle  rotation; if (diff > Mathf.PI) { destAngle=Mathf.PI*2; } else if (diff<Mathf.PI) { destAngle+=Mathf.PI*2; } if (Mathf.Abs(destAnglerotation)<=movingRotationSpeed) { rotation=destAngle } else { if (destAngle  rotation <= 0) { rotation = movingRotationSpeed; } else { rotation += movingRotationSpeed; } } [/quote] That code almost worked perfectly. But I can't explain how it's not working... as it seems to change. It seems that I can only draw two circles in any direction, and then it'll randomly draw the circle in the opposite direction. For example, clockwise, clockwise, attempt to go clockwise again but it turns counterclockwise... but I can draw figures of 8 indefinitely. I'm guessing I need to "wrap" the angle? private float WrapAngle(float theta) { if (theta < Math.PI) { theta += (float)(Math.PI * 2); } if (theta > Math.PI) { theta = (float)(Math.PI * 2); } return theta; } EDIT: The issue arises when the angle reaches ~8 (positive or negative). The rotation than starts dropping, rather than wrapping or just getting higher. 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
[background=rgb(250, 251, 252)]Sorry, I was editing that post as you wrote. I managed to successfully translate the code, after Googling all the syntax I didn't understand. But thank you for also clarifying.[/background] ___ If I use the while loop as provided in the code, the ship will instantly turn to face the cursor with no "turning" actually present. It just always looks at the cursor. However, while using the whileloop, the rotation itself is perfect. However, changing the while to an ifloop gives me the "turning" I'm looking for. However, the issue I had with my own code is exactly the same in this code. Upon attempting to cross the westaxis, the ship attempts to do a 359degree spin in the opposite direction. With forward motion enabled (rather than a stationary sprite), the ship just ~~~~~~~~ in a straight line. 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
While I'm thankful for it, I don't understand your help. It's both in a different language, and I'm at a loss to translate it because I don't know what <<, >>, cin, cout, etc, do. 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
I'm not sure I understand this syntax? But that aside. I've disabled forward motion for the purposes of testing without it going off the screen. Your solution has seemingly worsened the problem. Whenever the destination is above the Xaxis, it turns to face left and then stops rotating. Now given that it is impossible for it to not rotate (there's no if condition where it doesn't rotate): that means that it reaches an argument where both statements are true, and the ship is turning up then down again instantly. 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham replied to Tom 'Rossy' Rosbotham's topic in For Beginners's Forum
Not what I mean at all. I'm asking for what calculation I need to compare the current angle and the destination angle in order to determine whether to rotate the ship clockwise or counterclockwise. 
How do I calculate if I should be adding or subtracting rotation?
Tom 'Rossy' Rosbotham posted a topic in For Beginners's Forum
(I'm an XNA/C# Newbie). I currently have an Asteroidsstyle character that is controlled by the mouse. However, I'm having an issue when my ship's movement would have it turn across the "west" line. This is my current code for rotating: if (destAngle  rotation <= 0) { rotation = movingRotationSpeed; } else { rotation += movingRotationSpeed; } However, an issue arises when the mouse crosses over the "west" line (with respect to the sprite). The destAngle changes from 3.140 to +3.140 (radians/PI). If I'm going clockwise at the time, the ship will then do a massive anticlockwise movement (and vice verse). What computation should I be doing to determine if the ship should move clockwise or anticlockwise given I have its current angle and the destination angle? Here is my entire code segment for that method: MouseState mouseState = Mouse.GetState(); Vector2 mouseLoc = new Vector2(mouseState.X, mouseState.Y); Vector2 direction = position  mouseLoc; destAngle = (float)(Math.Atan2(direction.Y, direction.X)); if (mouseState.LeftButton == ButtonState.Pressed) { Console.WriteLine(rotation + " + " + destAngle); if (destAngle  rotation <= 0) { rotation = movingRotationSpeed; } else { rotation += movingRotationSpeed; } if (position.X == mouseState.X && position.Y == mouseState.Y) { } else { //The ship always moves "forward". position.X += speed * (float)(Math.Cos(rotation)); position.Y += speed * (float)(Math.Sin(rotation)); } }

Advertisement