GameDev.net Posting Guidelines (please read before posting)
For Beginners Forum FAQs (please read before posting)
Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 27 July 2013 - 03:06 PM
CEO of Dynamic Realities
Posted 27 July 2013 - 03:27 PM
Edited by MarkS, 27 July 2013 - 03:28 PM.
Posted 27 July 2013 - 03:33 PM
Dunno what Atan2 returns for 0, 0 in Java (EDIT: C# even ;)). In C/C++ it returns 0 and sets errno I think. You have to set it to some value, usually the last valid rotation is the best. Better still is to restrict the cursor so it can't get too close to the rotation centre.
Edited by Paradigm Shifter, 27 July 2013 - 03:47 PM.
Posted 27 July 2013 - 03:45 PM
Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.
I don't think you read the post or the code :/
I am already doing that.
CEO of Dynamic Realities
Posted 27 July 2013 - 03:47 PM
Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.
I don't think you read the post or the code :/
I am already doing that.
if fabs(old_pos - mouse_pos) > epsilon then old_pos = mouse_pos directionV = Normalize(mouse_pos- sp.Position); else directionV = Normalize(old_pos- sp.Position); endif newRot = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90;
Edited by MarkS, 27 July 2013 - 03:52 PM.
Posted 27 July 2013 - 04:02 PM
Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.
I don't think you read the post or the code :/
I am already doing that.
No, you are tracking the rotation. Track the mouse position.if fabs(old_pos - mouse_pos) > epsilon then old_pos = mouse_pos directionV = Normalize(mouse_pos- sp.Position); else directionV = Normalize(old_pos- sp.Position); endif newRot = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90;
Can't really see how I should translate this into C# given a mouse position is given by a Vector2i and a position is given by a Vector2f and by subtracting them you can't get a single number you can compare to Epsilon. Epsilon would have to be either vector itself to test for "is greater than" or the subtractions of the vectors have to be made into a number.
CEO of Dynamic Realities
Posted 27 July 2013 - 04:14 PM
distance = sqrt(((old_pos.x - mouse_pos.x) * (old_pos.x - mouse_pos.x)) + ((old_pos.y - mouse_pos.y) * (old_pos.y - mouse_pos.y))); if fabs(distance) > epsilon then ...
Edited by MarkS, 27 July 2013 - 04:17 PM.
Posted 27 July 2013 - 04:23 PM
Yeah, you meant "length" or "magnitude" rather than fabs.
And you want to use the square of the distance in your new example (rather than call sqrt, for efficiency reasons) and compare it to epsilon * epsilon, since if a >= 0 and b >= 0 then a >= b if and only if a^{2 }>= b^{2}
Edited by Paradigm Shifter, 27 July 2013 - 04:24 PM.
Posted 27 July 2013 - 04:34 PM
That is just pseudo-code. Find the distance between the two points and compare the distance to epsilon.
distance = sqrt(((old_pos.x - mouse_pos.x) * (old_pos.x - mouse_pos.x)) + ((old_pos.y - mouse_pos.y) * (old_pos.y - mouse_pos.y))); if fabs(distance) > epsilon then ...
It ended up like this:
CEO of Dynamic Realities
Posted 27 July 2013 - 04:42 PM
My guess is that Math.E is teeny weeny and not the value you want to test against. You want a buffer of at least a few pixels each side of the centre of rotation. Is it the floating point epsilon? You are testing distance in pixels and my guess is the epsilon is something like 10 to the minus 6? (It's either gonna be the floating point epsilon, usually around 10^{-6}, or else it is exp(1), the base of natural logarithms, which is about 2.718? neither of which would be correct for your purposes). EDIT: I think it is probably Exp(1), i.e. the constant e, which is 2.7 ish, so you are testing whether the mouse is within 2.7 pixels of the centre, probably not enough.
And there's no reason to call Sqrt, test the length squared against the distance (what you call epsilon) squared. The distance is the number of pixels you want the dead zone around the centre of rotation to be.
Edited by Paradigm Shifter, 27 July 2013 - 04:46 PM.
Posted 27 July 2013 - 04:47 PM
My guess is that Math.E is teeny weeny and not the value you want to test against. You want a buffer of at least a few pixels each side of the centre of rotation. Is it the floating point epsilon? You are testing distance in pixels and my guess is the epsilon is something like 10 to the minus 6? (It's either gonna be the floating point epsilon, usually around 10^{-6}, or else it is exp(1), the base of natural logarithms, which is about 2.718? neither of which would be correct for your purposes). EDIT: I think it is probably Exp(1), i.e. the constant e, which is 2.7 ish, so you are testing whether the mouse is within 2.7 pixels of the centre, probably not enough.
And there's no reason to call Sqrt, test the length squared against the distance (what you call epsilon) squared. The distance is the number of pixels you want the dead zone around the centre of rotation to be.
I moved the Sqrt() call out of the distance call and squarred epsilon instead. The behaviour is the same except for the lag which is gone now. Thanks. This thread have been helpful.
CEO of Dynamic Realities
Posted 27 July 2013 - 04:48 PM
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SFML.Graphics; using SFML.Window; namespace ComponentEngine { class Program { static RenderWindow window; static Sprite sp; static Vector2i currentMousePos,old_pos; static float speed; static void Main(string[] args) { INIParser iniparser = new INIParser(); Dictionary<String, int> VideoSettings = iniparser.ParseINI(); window = new RenderWindow(new VideoMode((uint)VideoSettings["width"], (uint)VideoSettings["height"], 32), "Component Engine"); sp = new Sprite(new Texture("car.png")); Vector2f pos = new Vector2f(); pos.X = window.Size.X / 2; pos.Y = window.Size.Y / 2; sp.Position = pos; sp.Origin = new Vector2f(sp.Texture.Size.X / 2, 0); currentMousePos = new Vector2i((int)window.Size.X / 2, (int)window.Size.Y / 2); speed = 1.0f; old_pos.X = 0.0f; old_pos.Y = 0.0f; while (window.IsOpen()) { if (Keyboard.IsKeyPressed(Keyboard.Key.Escape)) window.Close(); currentMousePos = window.InternalGetMousePosition(); window.DispatchEvents(); window.Clear(); MoveSprite(currentMousePos); window.Draw(sp); window.Display(); } } private static void MoveSprite(Vector2i mousepos) { Vector2f targetV = new Vector2f(mousepos.X, mousepos.Y); Vector2f directionV = new Vector2f(0.0f,0.0f); float distance = Math.Sqrt(((old_pos.X - mousepos.X) * (old_pos.X - mousepos.X)) + ((old_pos.Y - mousepos.Y) * (old_pos.Y - mousepos.Y))); // *NOTE* "epsilon" should be a value that you choose through testing. Find the value that works best. if(Math.Abs(distance) > epsilon) { old_pos = targetV; directionV = Normalize(targetV - sp.Position); }else{ directionV = Normalize(old_pos - sp.Position); } sp.Rotation = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90; Console.WriteLine((mousepos.X - sp.Position.X) + "," + (mousepos.Y - sp.Position.Y)); sp.Position += directionV * speed; } private static Vector2f Normalize(Vector2f v1) { float length = (float)Math.Sqrt((v1.X * v1.X) + (v1.Y * v1.Y)); float x = v1.X / length; float y = v1.Y / length; return new Vector2f(x, y); } } }
Edited by MarkS, 27 July 2013 - 04:56 PM.
Posted 27 July 2013 - 05:10 PM
Also, you don't need to normalize the argument vector to Atan2, it just divides them anyway and calls Atan(y/x) behind the scenes, so Atan2(k * y, k * x) is going to give the same answer as Atan2(y, x)
Edited by Paradigm Shifter, 27 July 2013 - 05:13 PM.
Posted 27 July 2013 - 05:51 PM
Yup, that looks good, nice one!
EDIT: Bites lip about you using degrees instead of radians and adding 90 degrees on to your angles instead of measuring them anticlockwise from the +X axis ;)
Edited by Paradigm Shifter, 27 July 2013 - 05:53 PM.
Posted 27 July 2013 - 05:57 PM
Yup, that looks good, nice one!
EDIT: Bites lip about you using degrees instead of radians and adding 90 degrees on to your angles instead of measuring them anticlockwise from the +X axis ;)
I guess I'll see if it comes back and bite me in the ass later Lol
CEO of Dynamic Realities
Posted 27 July 2013 - 06:19 PM
It won't, but it's just easier working in the correct units for the functions, which in the case of angles is radians, since that makes the maths easier. You aren't gonna measure the speed of your car in furlongs per fortnight are you? As soon as you start doing calculus you should forget about degrees entirely. The only other sensible option is wangs, wide angles, which are like wchars but have 65536 wangs in a full circle instead of 2pi radians, since they play nice with integer overflow on computers ;)
[note: I just made up wangs but I like it as a terminology ;)]
And if you are asked to draw a right angled triangle with an angle of 45 degrees, you measure the angle anticlockwise from the x axis don't you? That's how all the trig functions are defined. I have no idea why computer scientists like measuring their angles clockwise from due North myself, maybe they want to be sailors?
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.