UCollisionFiltering.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:5k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit UCollisionFiltering;
  2. interface
  3. {$I ......SourcePhysics2D.inc}
  4. uses
  5.    UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
  6. type
  7.    TCollisionFiltering = class(TTester)
  8.    public
  9.       constructor Create; override;
  10.    end;
  11. implementation
  12. const
  13.    // This is a test of collision filtering.
  14.    // There is a triangle, a box, and a circle.
  15.    // There are 6 shapes. 3 large and 3 small.
  16.    // The 3 small ones always collide.
  17.    // The 3 large ones never collide.
  18.    // The boxes don't collide with triangles (except if both are small).
  19.    k_smallGroup = 1;
  20.    k_largeGroup = -1;
  21.    k_defaultCategory = $0001;
  22.    k_triangleCategory = $0002;
  23.    k_boxCategory = $0004;
  24.    k_circleCategory = $0008;
  25.    k_triangleMask = $FFFF;
  26.    k_boxMask = $FFFF xor k_triangleCategory;
  27.    k_circleMask = $FFFF;
  28. { TCollisionFiltering }
  29. constructor TCollisionFiltering.Create;
  30. var
  31.    sd, boxShapeDef: Tb2PolygonDef;
  32.    bd, triangleBodyDef, boxBodyDef, circleBodyDef: Tb2BodyDef;
  33.    ground, body1, body2, body3, body4, body5, body6: Tb2Body;
  34.    triangleShapeDef: Tb2PolygonDef;
  35.    circleShapeDef: Tb2CircleDef;
  36. begin
  37.    inherited;
  38.    // Ground body
  39.    begin
  40.       sd := Tb2PolygonDef.Create;
  41.       sd.SetAsBox(50.0, 10.0);
  42.       sd.friction := 0.3;
  43.       bd := Tb2BodyDef.Create;
  44.       {$IFDEF OP_OVERLOAD}
  45.       bd.position.SetValue(0.0, -10.0);
  46.       {$ELSE}
  47.       SetValue(bd.position, 0.0, -10.0);
  48.       {$ENDIF}
  49.       ground := m_world.CreateBody(bd);
  50.       ground.CreateShape(sd);
  51.    end;
  52.    // Small triangle
  53.    triangleShapeDef := Tb2PolygonDef.Create;
  54.    triangleShapeDef.vertexCount := 3;
  55.    {$IFDEF OP_OVERLOAD}
  56.    triangleShapeDef.vertices[0].SetValue(-1.0, 0.0);
  57.    triangleShapeDef.vertices[1].SetValue(1.0, 0.0);
  58.    triangleShapeDef.vertices[2].SetValue(0.0, 2.0);
  59.    {$ELSE}
  60.    SetValue(triangleShapeDef.vertices[0], -1.0, 0.0);
  61.    SetValue(triangleShapeDef.vertices[1], 1.0, 0.0);
  62.    SetValue(triangleShapeDef.vertices[2], 0.0, 2.0);
  63.    {$ENDIF}
  64.    triangleShapeDef.density := 1.0;
  65.    triangleShapeDef.filter.groupIndex := k_smallGroup;
  66.    triangleShapeDef.filter.categoryBits := k_triangleCategory;
  67.    triangleShapeDef.filter.maskBits := k_triangleMask;
  68.    triangleBodyDef := Tb2BodyDef.Create;
  69.    {$IFDEF OP_OVERLOAD}
  70.    triangleBodyDef.position.SetValue(-5.0, 2.0);
  71.    {$ELSE}
  72.    SetValue(triangleBodyDef.position, -5.0, 2.0);
  73.    {$ENDIF}
  74.    body1 := m_world.CreateBody(triangleBodyDef, False);
  75.    body1.CreateShape(triangleShapeDef, False);
  76.    body1.SetMassFromShapes;
  77.    // Large triangle (recycle definitions)
  78.    {$IFDEF OP_OVERLOAD}
  79.    triangleShapeDef.vertices[0] := triangleShapeDef.vertices[0] * 2.0;
  80.    triangleShapeDef.vertices[1] := triangleShapeDef.vertices[1] * 2.0;
  81.    triangleShapeDef.vertices[2] := triangleShapeDef.vertices[2] * 2.0;
  82.    {$ELSE}
  83.    triangleShapeDef.vertices[0] := Multiply(triangleShapeDef.vertices[0], 2.0);
  84.    triangleShapeDef.vertices[1] := Multiply(triangleShapeDef.vertices[1], 2.0);
  85.    triangleShapeDef.vertices[2] := Multiply(triangleShapeDef.vertices[2], 2.0);
  86.    {$ENDIF}
  87.    triangleShapeDef.filter.groupIndex := k_largeGroup;
  88.    {$IFDEF OP_OVERLOAD}
  89.    triangleBodyDef.position.SetValue(-5.0, 6.0);
  90.    {$ELSE}
  91.    SetValue(triangleBodyDef.position, -5.0, 6.0);
  92.    {$ENDIF}
  93.    triangleBodyDef.fixedRotation := True; // look at me!
  94.    body2 := m_world.CreateBody(triangleBodyDef);
  95.    body2.CreateShape(triangleShapeDef);
  96.    body2.SetMassFromShapes;
  97.    // Small box
  98.    boxShapeDef := Tb2PolygonDef.Create;
  99.    boxShapeDef.SetAsBox(1.0, 0.5);
  100.    boxShapeDef.density := 1.0;
  101.    boxShapeDef.filter.groupIndex := k_smallGroup;
  102.    boxShapeDef.filter.categoryBits := k_boxCategory;
  103.    boxShapeDef.filter.maskBits := k_boxMask;
  104.    boxBodyDef := Tb2BodyDef.Create;
  105.    {$IFDEF OP_OVERLOAD}
  106.    boxBodyDef.position.SetValue(0.0, 2.0);
  107.    {$ELSE}
  108.    SetValue(boxBodyDef.position, 0.0, 2.0);
  109.    {$ENDIF}
  110.    body3 := m_world.CreateBody(boxBodyDef, False);
  111.    body3.CreateShape(boxShapeDef, False);
  112.    body3.SetMassFromShapes;
  113.    // Large box (recycle definitions)
  114.    boxShapeDef.SetAsBox(2.0, 1.0);
  115.    boxShapeDef.filter.groupIndex := k_largeGroup;
  116.    {$IFDEF OP_OVERLOAD}
  117.    boxBodyDef.position.SetValue(0.0, 6.0);
  118.    {$ELSE}
  119.    SetValue(boxBodyDef.position, 0.0, 6.0);
  120.    {$ENDIF}
  121.    body4 := m_world.CreateBody(boxBodyDef);
  122.    body4.CreateShape(boxShapeDef);
  123.    body4.SetMassFromShapes;
  124.    // Small circle
  125.    circleShapeDef := Tb2CircleDef.Create;
  126.    circleShapeDef.radius := 1.0;
  127.    circleShapeDef.density := 1.0;
  128.    circleShapeDef.filter.groupIndex := k_smallGroup;
  129.    circleShapeDef.filter.categoryBits := k_circleCategory;
  130.    circleShapeDef.filter.maskBits := k_circleMask;
  131.    circleBodyDef := Tb2BodyDef.Create;
  132.    {$IFDEF OP_OVERLOAD}
  133.    circleBodyDef.position.SetValue(5.0, 2.0);
  134.    {$ELSE}
  135.    SetValue(circleBodyDef.position, 5.0, 2.0);
  136.    {$ENDIF}
  137.    body5 := m_world.CreateBody(circleBodyDef, False);
  138.    body5.CreateShape(circleShapeDef, False);
  139.    body5.SetMassFromShapes;
  140.    // Large circle
  141.    circleShapeDef.radius :=  circleShapeDef.radius * 2.0;
  142.    circleShapeDef.filter.groupIndex := k_largeGroup;
  143.    {$IFDEF OP_OVERLOAD}
  144.    circleBodyDef.position.SetValue(5.0, 6.0);
  145.    {$ELSE}
  146.    SetValue(circleBodyDef.position, 5.0, 6.0);
  147.    {$ENDIF}
  148.    body6 := m_world.CreateBody(circleBodyDef);
  149.    body6.CreateShape(circleShapeDef);
  150.    body6.SetMassFromShapes;
  151. end;
  152. initialization
  153.    RegisterTestEntry('Collision Filtering', TCollisionFiltering);
  154. end.