UCollisionFiltering.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:5k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UCollisionFiltering;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain, UPhysics2DTypes, UPhysics2D, SysUtils;
- type
- TCollisionFiltering = class(TTester)
- public
- constructor Create; override;
- end;
- implementation
- const
- // This is a test of collision filtering.
- // There is a triangle, a box, and a circle.
- // There are 6 shapes. 3 large and 3 small.
- // The 3 small ones always collide.
- // The 3 large ones never collide.
- // The boxes don't collide with triangles (except if both are small).
- k_smallGroup = 1;
- k_largeGroup = -1;
- k_defaultCategory = $0001;
- k_triangleCategory = $0002;
- k_boxCategory = $0004;
- k_circleCategory = $0008;
- k_triangleMask = $FFFF;
- k_boxMask = $FFFF xor k_triangleCategory;
- k_circleMask = $FFFF;
- { TCollisionFiltering }
- constructor TCollisionFiltering.Create;
- var
- sd, boxShapeDef: Tb2PolygonDef;
- bd, triangleBodyDef, boxBodyDef, circleBodyDef: Tb2BodyDef;
- ground, body1, body2, body3, body4, body5, body6: Tb2Body;
- triangleShapeDef: Tb2PolygonDef;
- circleShapeDef: Tb2CircleDef;
- begin
- inherited;
- // Ground body
- begin
- sd := Tb2PolygonDef.Create;
- sd.SetAsBox(50.0, 10.0);
- sd.friction := 0.3;
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(0.0, -10.0);
- {$ELSE}
- SetValue(bd.position, 0.0, -10.0);
- {$ENDIF}
- ground := m_world.CreateBody(bd);
- ground.CreateShape(sd);
- end;
- // Small triangle
- triangleShapeDef := Tb2PolygonDef.Create;
- triangleShapeDef.vertexCount := 3;
- {$IFDEF OP_OVERLOAD}
- triangleShapeDef.vertices[0].SetValue(-1.0, 0.0);
- triangleShapeDef.vertices[1].SetValue(1.0, 0.0);
- triangleShapeDef.vertices[2].SetValue(0.0, 2.0);
- {$ELSE}
- SetValue(triangleShapeDef.vertices[0], -1.0, 0.0);
- SetValue(triangleShapeDef.vertices[1], 1.0, 0.0);
- SetValue(triangleShapeDef.vertices[2], 0.0, 2.0);
- {$ENDIF}
- triangleShapeDef.density := 1.0;
- triangleShapeDef.filter.groupIndex := k_smallGroup;
- triangleShapeDef.filter.categoryBits := k_triangleCategory;
- triangleShapeDef.filter.maskBits := k_triangleMask;
- triangleBodyDef := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- triangleBodyDef.position.SetValue(-5.0, 2.0);
- {$ELSE}
- SetValue(triangleBodyDef.position, -5.0, 2.0);
- {$ENDIF}
- body1 := m_world.CreateBody(triangleBodyDef, False);
- body1.CreateShape(triangleShapeDef, False);
- body1.SetMassFromShapes;
- // Large triangle (recycle definitions)
- {$IFDEF OP_OVERLOAD}
- triangleShapeDef.vertices[0] := triangleShapeDef.vertices[0] * 2.0;
- triangleShapeDef.vertices[1] := triangleShapeDef.vertices[1] * 2.0;
- triangleShapeDef.vertices[2] := triangleShapeDef.vertices[2] * 2.0;
- {$ELSE}
- triangleShapeDef.vertices[0] := Multiply(triangleShapeDef.vertices[0], 2.0);
- triangleShapeDef.vertices[1] := Multiply(triangleShapeDef.vertices[1], 2.0);
- triangleShapeDef.vertices[2] := Multiply(triangleShapeDef.vertices[2], 2.0);
- {$ENDIF}
- triangleShapeDef.filter.groupIndex := k_largeGroup;
- {$IFDEF OP_OVERLOAD}
- triangleBodyDef.position.SetValue(-5.0, 6.0);
- {$ELSE}
- SetValue(triangleBodyDef.position, -5.0, 6.0);
- {$ENDIF}
- triangleBodyDef.fixedRotation := True; // look at me!
- body2 := m_world.CreateBody(triangleBodyDef);
- body2.CreateShape(triangleShapeDef);
- body2.SetMassFromShapes;
- // Small box
- boxShapeDef := Tb2PolygonDef.Create;
- boxShapeDef.SetAsBox(1.0, 0.5);
- boxShapeDef.density := 1.0;
- boxShapeDef.filter.groupIndex := k_smallGroup;
- boxShapeDef.filter.categoryBits := k_boxCategory;
- boxShapeDef.filter.maskBits := k_boxMask;
- boxBodyDef := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- boxBodyDef.position.SetValue(0.0, 2.0);
- {$ELSE}
- SetValue(boxBodyDef.position, 0.0, 2.0);
- {$ENDIF}
- body3 := m_world.CreateBody(boxBodyDef, False);
- body3.CreateShape(boxShapeDef, False);
- body3.SetMassFromShapes;
- // Large box (recycle definitions)
- boxShapeDef.SetAsBox(2.0, 1.0);
- boxShapeDef.filter.groupIndex := k_largeGroup;
- {$IFDEF OP_OVERLOAD}
- boxBodyDef.position.SetValue(0.0, 6.0);
- {$ELSE}
- SetValue(boxBodyDef.position, 0.0, 6.0);
- {$ENDIF}
- body4 := m_world.CreateBody(boxBodyDef);
- body4.CreateShape(boxShapeDef);
- body4.SetMassFromShapes;
- // Small circle
- circleShapeDef := Tb2CircleDef.Create;
- circleShapeDef.radius := 1.0;
- circleShapeDef.density := 1.0;
- circleShapeDef.filter.groupIndex := k_smallGroup;
- circleShapeDef.filter.categoryBits := k_circleCategory;
- circleShapeDef.filter.maskBits := k_circleMask;
- circleBodyDef := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- circleBodyDef.position.SetValue(5.0, 2.0);
- {$ELSE}
- SetValue(circleBodyDef.position, 5.0, 2.0);
- {$ENDIF}
- body5 := m_world.CreateBody(circleBodyDef, False);
- body5.CreateShape(circleShapeDef, False);
- body5.SetMassFromShapes;
- // Large circle
- circleShapeDef.radius := circleShapeDef.radius * 2.0;
- circleShapeDef.filter.groupIndex := k_largeGroup;
- {$IFDEF OP_OVERLOAD}
- circleBodyDef.position.SetValue(5.0, 6.0);
- {$ELSE}
- SetValue(circleBodyDef.position, 5.0, 6.0);
- {$ENDIF}
- body6 := m_world.CreateBody(circleBodyDef);
- body6.CreateShape(circleShapeDef);
- body6.SetMassFromShapes;
- end;
- initialization
- RegisterTestEntry('Collision Filtering', TCollisionFiltering);
- end.