UContactCallback.pas
上传用户:zkjn0718
上传日期:2021-01-01
资源大小:776k
文件大小:8k
源码类别:
Delphi/CppBuilder
开发平台:
Delphi
- unit UContactCallback;
- interface
- {$I ......SourcePhysics2D.inc}
- uses
- UMain,
- UPhysics2DTypes,
- UPhysics2D,
- SysUtils,
- Classes;
- type
- TContactCallback = class(TTester)
- public
- m_ball, m_bullet: Tb2Body;
- m_ball_shape: Tb2Shape;
- m_strings: TStringList;
- constructor Create; override;
- destructor Destroy; override;
- procedure Step(var settings: TSettings; timeStep: Float); override;
- end;
- implementation
- { TContactCallback }
- constructor TContactCallback.Create;
- var
- sd, cd: Tb2PolygonDef;
- bd: Tb2BodyDef;
- w, b, s: Float;
- begin
- inherited;
- m_strings := TStringList.Create;
- sd := Tb2PolygonDef.Create;
- sd.friction := 0;
- sd.vertexCount := 3;
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(10, 10);
- sd.vertices[1].SetValue(9, 7);
- sd.vertices[2].SetValue(10, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 10, 10);
- SetValue(sd.vertices[1], 9, 7);
- SetValue(sd.vertices[2], 10, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(9, 7);
- sd.vertices[1].SetValue(8, 0);
- sd.vertices[2].SetValue(10, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 9, 7);
- SetValue(sd.vertices[1], 8, 0);
- SetValue(sd.vertices[2], 10, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(9, 7);
- sd.vertices[1].SetValue(8, 5);
- sd.vertices[2].SetValue(8, 0);
- {$ELSE}
- SetValue( sd.vertices[0], 9, 7);
- SetValue(sd.vertices[1], 8, 5);
- SetValue(sd.vertices[2], 8, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(8, 5);
- sd.vertices[1].SetValue(7, 4);
- sd.vertices[2].SetValue(8, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 8, 5);
- SetValue(sd.vertices[1], 7, 4);
- SetValue(sd.vertices[2], 8, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(7, 4);
- sd.vertices[1].SetValue(5, 0);
- sd.vertices[2].SetValue(8, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 7, 4);
- SetValue(sd.vertices[1], 5, 0);
- SetValue(sd.vertices[2], 8, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(7, 4);
- sd.vertices[1].SetValue(5, 3);
- sd.vertices[2].SetValue(5, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 7, 4);
- SetValue(sd.vertices[1], 5, 3);
- SetValue(sd.vertices[2], 5, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(5, 3);
- sd.vertices[1].SetValue(2, 2);
- sd.vertices[2].SetValue(5, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 5, 3);
- SetValue(sd.vertices[1], 2, 2);
- SetValue(sd.vertices[2], 5, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(2, 2);
- sd.vertices[1].SetValue(0, 0);
- sd.vertices[2].SetValue(5, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 2, 2);
- SetValue(sd.vertices[1], 0, 0);
- SetValue(sd.vertices[2], 5, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[0].SetValue(2, 2);
- sd.vertices[1].SetValue(-2, 2);
- sd.vertices[2].SetValue(0, 0);
- {$ELSE}
- SetValue(sd.vertices[0], 2, 2);
- SetValue(sd.vertices[1], -2, 2);
- SetValue(sd.vertices[2], 0, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-2, 2);
- sd.vertices[1].SetValue(0, 0);
- sd.vertices[0].SetValue(-5, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -2, 2);
- SetValue(sd.vertices[1], 0, 0);
- SetValue(sd.vertices[0], -5, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-5, 3);
- sd.vertices[1].SetValue(-2, 2);
- sd.vertices[0].SetValue(-5, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -5, 3);
- SetValue(sd.vertices[1], -2, 2);
- SetValue(sd.vertices[0], -5, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-7, 4);
- sd.vertices[1].SetValue(-5, 3);
- sd.vertices[0].SetValue(-5, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -7, 4);
- SetValue(sd.vertices[1], -5, 3);
- SetValue(sd.vertices[0], -5, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-7, 4);
- sd.vertices[1].SetValue(-5, 0);
- sd.vertices[0].SetValue(-8, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -7, 4);
- SetValue(sd.vertices[1], -5, 0);
- SetValue(sd.vertices[0], -8, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-8, 5);
- sd.vertices[1].SetValue(-7, 4);
- sd.vertices[0].SetValue(-8, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -8, 5);
- SetValue(sd.vertices[1], -7, 4);
- SetValue(sd.vertices[0], -8, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-9, 7);
- sd.vertices[1].SetValue(-8, 5);
- sd.vertices[0].SetValue(-8, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -9, 7);
- SetValue(sd.vertices[1], -8, 5);
- SetValue(sd.vertices[0], -8, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-9, 7);
- sd.vertices[1].SetValue(-8, 0);
- sd.vertices[0].SetValue(-10, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -9, 7);
- SetValue(sd.vertices[1], -8, 0);
- SetValue(sd.vertices[0], -10, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- {$IFDEF OP_OVERLOAD}
- sd.vertices[2].SetValue(-10, 10);
- sd.vertices[1].SetValue(-9, 7);
- sd.vertices[0].SetValue(-10, 0);
- {$ELSE}
- SetValue(sd.vertices[2], -10, 10);
- SetValue(sd.vertices[1], -9, 7);
- SetValue(sd.vertices[0], -10, 0);
- {$ENDIF}
- m_world.GetGroundBody.CreateShape(sd, False);
- sd.SetAsBox(0.5, 6, MakeVector(10.5, 6), 0);
- m_world.GetGroundBody.CreateShape(sd, False);
- sd.SetAsBox(0.5, 6, MakeVector(-10.5, 6), 0);
- m_world.GetGroundBody.CreateShape(sd);
- bd := Tb2BodyDef.Create;
- {$IFDEF OP_OVERLOAD}
- bd.position.SetValue(9.5, 60);
- {$ELSE}
- SetValue(bd.position, 9.5, 60);
- {$ENDIF}
- m_ball := m_world.CreateBody(bd);
- cd := Tb2PolygonDef.Create;
- cd.vertexCount := 8;
- w := 1.0;
- b := w / (2.0 + Sqrt(2.0));
- s := Sqrt(2.0) * b;
- {$IFDEF OP_OVERLOAD}
- cd.vertices[0].SetValue(0.5 * s, 0.0);
- cd.vertices[1].SetValue(0.5 * w, b);
- cd.vertices[2].SetValue(0.5 * w, b + s);
- cd.vertices[3].SetValue(0.5 * s, w);
- cd.vertices[4].SetValue(-0.5 * s, w);
- cd.vertices[5].SetValue(-0.5 * w, b + s);
- cd.vertices[6].SetValue(-0.5 * w, b);
- cd.vertices[7].SetValue(-0.5 * s, 0.0);
- {$ELSE}
- SetValue(cd.vertices[0], 0.5 * s, 0.0);
- SetValue(cd.vertices[1], 0.5 * w, b);
- SetValue(cd.vertices[2], 0.5 * w, b + s);
- SetValue(cd.vertices[3], 0.5 * s, w);
- SetValue(cd.vertices[4], -0.5 * s, w);
- SetValue(cd.vertices[5], -0.5 * w, b + s);
- SetValue(cd.vertices[6], -0.5 * w, b);
- SetValue(cd.vertices[7], -0.5 * s, 0.0);
- {$ENDIF}
- cd.density := 1.0;
- m_ball_shape := m_ball.CreateShape(cd);
- m_ball.SetMassFromShapes;
- end;
- destructor TContactCallback.Destroy;
- begin
- m_strings.Free;
- inherited;
- end;
- procedure TContactCallback.Step(var settings: TSettings; timeStep: Float);
- var
- i: Integer;
- begin
- inherited;
- for i := 0 to m_pointCount - 1 do
- with m_points[i] do
- begin
- case state of
- e_contactAdded: m_strings.Add(Format('added: %d -> %d : %d', [Integer(shape1), Integer(shape2), id.key]));
- e_contactRemoved: m_strings.Add(Format('removed: %d -> %d : %d', [Integer(shape1), Integer(shape2), id.key]));
- end;
- end;
- while (m_strings.Count > 15) do
- m_strings.Delete(0);
- for i := 0 to m_strings.Count - 1 do
- DrawText(m_strings[i]);
- end;
- initialization
- RegisterTestEntry('Contact Callback', TContactCallback);
- end.