I'm using Processing to program a 2D mini game. There is a car, you can control, and a cross slowly chasing after the car. Fairly simple. But now to my question.
How do I use only a single Parameter to calculate, in which direction the cross should move?
This is the calculation of the distance from Point A to Point B
float distance ( float xa, float ya, float xb, float yb )
{
float dx = xa-xb;
float dy = ya-yb;
float dist = sqrt(dx*dx+dy*dy);
return dist;
}
To keep it Simple, there must be a possible and simple solution, right?
Processing has a dist()
function that you should probably use instead of doing it yourself. More info can be found in the reference.
I'm not exactly sure what you mean by single parameter, because you're going to need an X speed and a Y speed, so it's at least two parameters. But there are a few ways to go about finding them:
You could scale your X and Y distances into a unit vector. The PVector
class has useful function that would help with that. Again, more info can be found in the reference.
Or you could use basic trigonometry to calculate the angle between the two objects. Google is your friend there. When you have the angle, you can use the cos()
and sin()
functions to calculate an X and Y speed.
Stack Overflow isn't really designed for general "how do I do this" type questions. It's for more specific "I tried X, expected Y, but got Z instead" type questions. So you should really try something, and post a MCVE if you get stuck. Good luck.