they are free to move anywhere on the screen weather in straight lines or curved ones just as long as they don’t be at the same place at any given time.
btw I’m not asking how to write it in code, but rather the logic if you will?
1- how would you make an object move randomly? (disregarding predefined functions or classes..etc)
2-how do you ensure they never hit?
2
From a physics perspective, collision is just an extreme case of a repulsive force between objects. If you want them to automatically avoid each other, a simple way would be to introduce a gradual force that repels them depending on how close they are to each other. For example, electrical forces satisfy
force ∝ 1 / distance^2
Since this doesn’t need to be physical, you can tweak the coefficient and/or exponent however you wish to make the objects better at avoiding each other. It doesn’t need to obey a power law either, you can introduce any complicated force field as long as it’s repulsive:
force = f(d) R
where d
is the separation distance, f
is a function that becomes increasingly large near 0
, and R
is the unit vector that defines the direction between the two objects.
From a different angle, if you can control the “randomness” of the motion, you can also influence it in such a way that they will never hit each other, but this requires your program to be able to predict the motion of other object.
There are many different kinds of “random” motion: you may want to be specific as to what kind you are interested in. If you want the motion to appear continuous and smooth, you can add random but small changes to the velocity vector (i.e. the magnitude and direction) at every frame of the animation. Or if you want it to be jerky, you can make the random changes more drastic.
If they can randomly move on a straight liner simply let both of them move randomly right and left while not being on the same height.