What I have currently is two objects that can be played though nav keys and the other one with wasd. The point is to get the 3rd object and score a point, and it randoms a new p
The easiest and most accurate way, is to rely on vector geometry.
Define class
public class Vector2 {
public float x {get;set;};
public float y {get;set;};
public Vector2(float a_x, float a_y, float b_x, float b_y)
{
x = b_x - a_x;
y = b_y - a_y;
}
//calculate vector length
public float Length {
get {
return Math.Sqrt((x * x) + (y * y));
}
}
public void SetVectorLength(float desiredLength){
double r = desiredLength/ this.Length;
this.x *= r;
this.y *= r;
}
}
this is just a set of functions for this case, but you can add others, plenty of them need for various parts of your calculations.
After what you have to do is just, calculate next position of your vertex toward the directional vector.
//hypothetical X coordinate
float x_coord = 1.34f;
//move towards the vector
var vector = new Vector2(10, 10, 20, 20);
//set vector move step
vector.SetLength(0.1f);
var movedX = x_coord + vector.x;
Repeat: this is just hypothetical example, you need to work on this. It's hard to put in small answer all related vector geometry stuff.
Try something like this
public void FlyttaMot(int x, int y, float speed)
{
float tx = x - pt.X;
float ty = y - pt.Y;
float length = Math.Sqrt(tx*tx + ty*ty);
if (length > speed)
{
// move towards the goal
pt.X = (int)(pt.X + speed* tx/length);
pt.Y = (int)(pt.Y + speed* ty/length);
}
else
{
// already there
pt.X = x;
pt.Y = y;
}
}
Call it from your timer tick code in Form.cs
like this for example.
npc2.FlyttaMot(200, 200, 0.5f);
I based this on linear algebra. Take a look at this video for example. The (tx,ty)
is the vector in which direction the npc should go. Dividing with the length of the vector gives us a vector of length 1. I multiply this with a speed parameter so you can adjust the speed of the npc.