Search code examples
math3dgame-enginegame-physics

How to show if two objects are moving away in 3D space


I am working on a yet another 3d game engine and have following Problem. In order to improve Performance I want to check if two Objects are coming closer or moving away from each other. Only if they are coming closer to each other collision detection should happen.

At the moment the code calculates the current distance using the positions of both Objects. Then moves both positions about the velocity vector and calculates the expected distance. etc.

public class Model{

    public boolean isApproaching(Model model) {
        float [] otherPosition = model.getPosition();
        float [] otherVelocity = model.getVelocity();
        double currentDistance = Math.pow(this.position[0] - otherPosition[0], 2)
                                + Math.pow(this.position[1] - otherPosition[1], 2)
                                + Math.pow(this.position[2] - otherPosition[2], 2);
        double expectedDistance = Math.pow(this.position[0] + this.velocityVector[0]/1000 - otherPosition[0] - otherVelocity[0]/1000, 2)
                                + Math.pow(this.position[1] + this.velocityVector[1]/1000 - otherPosition[1] - otherVelocity[1]/1000, 2)
                                + Math.pow(this.position[2] + this.velocityVector[2]/1000 - otherPosition[2] - otherVelocity[2]/1000, 2);
        if(currentDistance < expectedDistance)
            return false;
        return true;
    }

}

As you can see in the Picture it does not work for fast moving objects. Is there any good way to show if two points are moving towards each other?

enter image description here


Solution

  • Given the two straight line trajectories, there's a unique time when they're going to be closest, which may have been in the past. If it's in the past then it means that the objects are moving away from each other, but if its in the future it means that they're moving closer together. But even it they're moving closer together, I would guess that knowing how soon is the information that you need, so that you can schedule when to worry about it. This methodology should cater for both slow moving and fast moving objects.