2d OBB OBB botsing detectie

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

2d OBB OBB botsing detectie

Berichtdoor bluatigro » vr jul 25, 2014 10:10 am

ik ben n c++ voorbeeld aan t vertalen

c++ :
Code: Selecteer alles
class OBB2D {
private:
    /** Corners of the box, where 0 is the lower left. */
    Vector2         corner[4];    /** Two edges of the box extended away from corner[0]. */
    Vector2         axis[2];    /** origin[a] = corner[0].dot(axis[a]); */
    double          origin[2];    /** Returns true if other overlaps one dimension of this. */
    bool overlaps1Way(const OBB2D& other) const {
        for (int a = 0; a < 2; ++a) {
            double t = other.corner[0].dot(axis[a]);            // Find the extent of box 2 on axis a
            double tMin = t;
            double tMax = t;
            for (int c = 1; c < 4; ++c) {
                t = other.corner[c].dot(axis[a]);
                if (t < tMin) {
                    tMin = t;
                } else if (t > tMax) {
                    tMax = t;
                }
            }            // We have to subtract off the origin

            // See if [tMin, tMax] intersects [0, 1]
            if ((tMin > 1 + origin[a]) || (tMax < origin[a])) {
                // There was no intersection along this dimension;
                // the boxes cannot possibly overlap.
                return false;
            }
        }        // There was no dimension along which there is no intersection.
        // Therefore the boxes overlap.
        return true;
    }
    /** Updates the axes after the corners move.  Assumes the
        corners actually form a rectangle. */
    void computeAxes() {
        axis[0] = corner[1] - corner[0];
        axis[1] = corner[3] - corner[0];         // Make the length of each axis 1/edge length so we know any
        // dot product must be less than 1 to fall within the edge.

        for (int a = 0; a < 2; ++a) {
            axis[a] /= axis[a].squaredLength();
            origin[a] = corner[0].dot(axis[a]);
        }
    }
public:
    OBB2D(const Vector2& center, const double w, const double h, double angle)
{
        Vector2 X( cos(angle), sin(angle));
        Vector2 Y(-sin(angle), cos(angle));
        X *= w / 2;
        Y *= h / 2;
        corner[0] = center - X - Y;
        corner[1] = center + X - Y;
        corner[2] = center + X + Y;
        corner[3] = center - X + Y;
        computeAxes();
    }
    /** For testing purposes. */
    void moveTo(const Vector2& center) {
        Vector2 centroid = (corner[0] + corner[1] + corner[2] + corner[3]) / 4;        Vector2 translation = center - centroid;        for (int c = 0; c < 4; ++c) {
            corner[c] += translation;
        }        computeAxes();
    }    /** Returns true if the intersection of the boxes is non-empty. */
    bool overlaps(const OBB2D& other) const {
        return overlaps1Way(other) && other.overlaps1Way(*this);
    }    void render() const {
        glBegin(GL_LINES);
            for (int c = 0; c < 5; ++c) {
              glVertex2fv(corner[c & 3]);
            }
        glEnd();
    }
}


ik ben zo ver gekomen
LB :
Code: Selecteer alles
dim obb( 20 , 13 )
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global pi , tx1 , ty1 , tx2 , ty2 , tx3 , ty3 , tx4 , ty4 , tax1 , tay1 , tax2 , tay2 , tcx , tcy
pi = atn( 1 ) * 4
tx1 = 0
ty1 = 1
tx2 = 2
ty2 = 3
tx3 = 4
ty3 = 5
tx4 = 6
ty4 = 7
tax1 = 8
tay1 = 9
tax2 = 10
tay2 = 11
tcx = 12
tcy = 13
global true , false
false = 0
true = not( false )


nomainwin
open "OBB 2D test" for graphics as #m
  #m "trapclose [quit]"
  call obb 0 , 800 , 350 , 30 , 50 , 30
  for i = 1 to 20
    call obb i , 200 , 350 , 30 , 50 , i * 18
  next i
  timer 40 , [tmr]
wait
[tmr]
  #m "cls"
  #m "fill black"
  for i = 1 to 20
    if not( hit( i , 0 ) ) then
      call move i , 1 , 0
    end if
    #m "color " ; rainbow$( i * 18 )
    call drawobb i
  next i
  #m "color white"
  call drawobb 0
wait
[quit]
  close #m
end
function rainbow$( x )
  rainbow$ = rgb$( sin( rad( x ) ) * 127 + 128 _
                 , sin( rad( x + 120 ) ) * 127 + 128 _
                 , sin( rad( x - 120 ) ) * 127 + 128 )
end function
function rgb$( r , g , b )
  rgb$ = str$( r and 255 ) ; " " ; g and 255 ; " " ; b and 255
end function
sub move no , dx , dy
  for i = 0 to 3
    obb(no,tx1+i*2) = obb(no,tx1+i*2) + dx
    obb(no,ty1+i*2) = obb(no,ty1+i*2) + dy
  next i
  call computeAxis no
end sub
function hit( a , b )
  hit = hit1way( a , b ) and hit1way( b , a )
end function
function hit1way( this , other )
  for a = 0 to 1
    tx = obb(this,tax1+a*2)
    ty = obb(this,tay1+a*2)
    ox = obb(other,tx1)
    oy = obb(other,ty1)
    t = dot( tx , ty , ox , oy )
    tmin = t
    tmax = t
    for c = 0 to 3
      cx = obb(other,tx1+c*2)
      cy = obb(other,ty1+c*2)
      t = dot( tx , ty , cx , cy )
      if t < tmin then
        tmin = t
      else
        if t > tmax then
          tmax = t
        end if
      end if
    next c
    if tmin > obb(this,tcx+a) or tmax < obb(this,tcx+a) then
      hit1way = false
      exit function
    end if
  next a
  hit1way = true
end function
sub drawobb no
  #m "down"
  #m "line " ; obb(no,tx1) ; " " ; obb(no,ty1) ; " " ; obb(no,tx2) ; " " ; obb(no,ty2)
  #m "line " ; obb(no,tx2) ; " " ; obb(no,ty2) ; " " ; obb(no,tx3) ; " " ; obb(no,ty3)
  #m "line " ; obb(no,tx3) ; " " ; obb(no,ty3) ; " " ; obb(no,tx4) ; " " ; obb(no,ty4)
  #m "line " ; obb(no,tx4) ; " " ; obb(no,ty4) ; " " ; obb(no,tx1) ; " " ; obb(no,ty1)
  #m "up"
end sub
function rad( x )
  rad = x * pi / 180
end function
sub obb no , cx , cy , w , h , angle
  ax = cos( rad( angle ) ) * w
  ay = 0-sin( rad( angle ) ) * w
  bx = sin( rad( angle ) ) * h
  by = cos( rad( angle ) ) * h
''compute corner[ 4 ]
  obb(no,tx1) = cx - ax - bx
  obb(no,ty1) = cy - ay - by
  obb(no,tx2) = cx - ax + bx
  obb(no,ty2) = cy - ay + by
  obb(no,tx3) = cx + ax + bx
  obb(no,ty3) = cy + ay + by
  obb(no,tx4) = cx + ax - bx
  obb(no,ty4) = cy + ay - by
  call computeAxis no
end sub
sub computeAxis no
  obb(no,tax1) = obb(no,tx2) - obb(no,tx1)
  obb(no,tay1) = obb(no,ty2) - obb(no,ty1)
  obb(no,tax2) = obb(no,tx4) - obb(no,tx1)
  obb(no,tay2) = obb(no,ty4) - obb(no,ty1)
  obb(no,tax1) = obb(no,tax1) / sqrlen( obb(no,tax1) , obb(no,tay1) )
  obb(no,tay1) = obb(no,tay1) / sqrlen( obb(no,tax1) , obb(no,tay1) )
  obb(no,tax2) = obb(no,tax2) / sqrlen( obb(no,tax2) , obb(no,tay2) )
  obb(no,tay2) = obb(no,tay2) / sqrlen( obb(no,tax2) , obb(no,tay2) )
''compute origen[ 2 ]
  x1 = obb(no,tax1)
  y1 = obb(no,tay1)
  x2 = obb(no,tax2)
  y2 = obb(no,tay2)
  x0 = obb(no,tx1)
  y0 = obb(no,ty1)
  obb(no,tcx) = dot( x0 , y0 , x1 , y1 )
  obb(no,tcy) = dot( x0 , y0 , x2 , y2 )
end sub
function sqrlen( x , y )
  sqrlen = x ^ 2 + y ^ 2
end function
function dot( ax , ay , bx , by )
  dot = ax * bx + ay * by
end function


t werkt maar is niet perfekt
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron