admin管理员组文章数量:1287882
The problem is that I can't make the object follow my mouse at a certain speed. I managed to make it follow the mouse, but it increases the speed the further away it's and get slower the closer it's.
I tried doing this
Xspeed = (mousePosition.X - object.Left) / 20;
Yspeed = (mousePosition.Y - object.Top) / 20;
x = x + Xspeed;
y = y + Yspeed;
object.location = new Point(x, y);
But it doesn't do what I want it to do.
The problem is that I can't make the object follow my mouse at a certain speed. I managed to make it follow the mouse, but it increases the speed the further away it's and get slower the closer it's.
I tried doing this
Xspeed = (mousePosition.X - object.Left) / 20;
Yspeed = (mousePosition.Y - object.Top) / 20;
x = x + Xspeed;
y = y + Yspeed;
object.location = new Point(x, y);
But it doesn't do what I want it to do.
Share Improve this question edited Feb 22 at 18:16 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 22 at 18:11 Žygimantas TamošaitisŽygimantas Tamošaitis 1 4- 1 Do the math. You made it, so the speed is a function over the distance, so why would you expect it to not be? If you want constant speed, you need a constant value to add. – Fildor Commented Feb 22 at 18:53
- 1 All you should be doing at "constant speed" is checking the "angle of advance" (and "arriving" or colliding). If it deviates from the target point (angle); you re-align or rotate. That's it. – Gerry Schmitz Commented Feb 22 at 19:08
- 1 Yeah so I can't add a constant value because it won't reach my mouse and it will go wherever the object wants (or I just don't understand). – Žygimantas Tamošaitis Commented Feb 22 at 20:08
- 1 It's going to go wherever it wants unless you make it go (and stop) where the last Mouse position was detected / signaled – Jimi Commented Feb 22 at 20:19
1 Answer
Reset to default 0First of, I would highly recommend using points/vectors for things like this, since it makes it much easier and removes the need to write out all the math by hand. I'm using System.Numerics.Vector2 for this example, but there are a bunch of vector libraries that would work fine, or you can write your own.
The basic idea is to separate the vector into a direction and speed component, and clamp the speed.
var objToMouse = mousePosition - object.location;
var speed = objToMouse.Length();
speed = Math.Min(speed, MaxSpeed); // clamp the speed
var deltaPos = Vector2.Normalize(objToMouse) * speed;
object.location += deltaPos;
Running this code in a timer should give you a maximal movement speed towards the mouse pointer. You might consider measuring the time between each timer tick using a stopwatch to make your speed independent of the timer interval, and to compensate for variations in the interval.
Note that you probably have to write some conversion code between winforms point
and whatever vector library you are using.
版权声明:本文标题:winforms - How to move an object to the mouse with a set max speed in Windows Forms using C# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741331288a2372778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论