Translation direction in separating axis theorem

风格不统一 提交于 2019-12-12 09:24:39

问题


I have implementen the separating axis theorem in java. The collision detection itself works great. But i am stuck when it comes to resolve the collision.

my method to get the translation looks like this:

public float getOverlap(final Projection other) {
    float start = m_min > other.m_min ? m_min : other.m_min;
    float end = m_max < other.m_max ? m_max : other.m_max;
    float translation = end - start;
    return translation;
}

Lets say the projection of the two rectangles in the picture looks like this.

R1.min = 2
R1.max = 8
R2.min = 5
R2.max = 11

When i check R1 vs. R2 the translation will be 3 When i checl R2 vs. R1 the translation also will be 3

now i add the translation to the normalized axis

Normalized axis = Vector(1,0)
Translation Vector = Vector(1,0)*3 = Vector (3,0)

And now R1 and R2 both move 3 points to the right, but they are suposed to move in different directions. R1 should move Vector(-3,0), R2 should move Vector(3,0).

how can i compute the right direction?


回答1:


My solution:

I substract the center vector R1 from center Vector R2, build the dot product to the tested axis and invert the translation if the dotproduct is smaller then 0

Vector centerR1(R1.x,R1.y);
Vector centerR2(R2.x,R2.y);

Vector R1toR2 = centerR2 - centerR1;

if(R1toR2.dot(axis)<0){
  translation = -translation
}

"When the vector (R1toR2) points in an negative direction, invert the translation"



来源:https://stackoverflow.com/questions/10396068/translation-direction-in-separating-axis-theorem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!