移动相交的对象,直到它们不再相交

时间:2013-02-27 17:54:42

标签: java collision-detection collision intersection

如果我还有相交的物体。

我如何知道我应该多少移动它们以阻止它们相互接触?

我知道如何判断哪些对象相交(使用java的intersects方法。

我还应该提到所有对象都在一个对象列表中(称为objs bellow)......

public void move(Object ob){
    // this rectangle surrounds the object I'll check against
    // all of the other objects below
    Rectangle objectToCheck = ob.getBounds();

    // Cycle through all the other objects and check if they
    // cross over the rectangle I created above
    for(Object obj : objs){
        // Creates a bounding rectangle that is used temporarily
        // for each other object on the board
        Rectangle otherObject= obj.getBounds();

        // Check if one object crosses over another object
        if(otherObject != objectToCheck && otherObject.intersects(objectToCheck)){
         // they intersect! now how do i know how much to move them so
         // that they no longer intersect? 
    } 

0 个答案:

没有答案