Download Now

    Reference TPhysicsScene

    Declared in Codebot.Render.Scenes.Physics.pas on line 57

    TPhysicsScene runs a Chipmunk2D space which is stepped every PhysicsStep seconds by the step thread of the TGraphicsBox it runs in. The scene controller starts the step thread after the scene is created, so the space can be built in Initialize without locking it.
    The space is shared by the step thread and the render thread. Outside of Simulate, wrap any code which uses the space in Lock and Unlock. DrawPhysics and the mouse handlers do this for you.
    Simulate, collision events, and post step callbacks run on the step thread and must not access LCL controls directly. If one of them raises an exception the step thread stops and the box reports it through its Failed property and OnFailed event.
    When GrabBodies is true dynamic bodies can be dragged with the left mouse button.
      TPhysicsScene = class(TWidgetScene)
      protected
        { Simulate is called on the step thread with the space locked. Override it
          to apply forces or game logic, and call inherited to step the space. }
        procedure Simulate(DeltaTime: Double); virtual;
        { Convert a scene point to a studio point. Override this together with
          ScaleToStudio to map the studio to the scene in another way. }
        function PointToStudio(X, Y: Float): TVec2; virtual;
        { Set the canvas matrix to draw in studio coordinates }
        procedure ScaleToStudio; virtual;
        { The nearest shape within a distance of a studio point or a nil shape }
        function ShapeNearPoint(X, Y, Distance: Float): TShape;
        { Add static side and ground walls around the studio }
        procedure GenerateStudioWalls;
        { Return true to disable default drawing }
        function DrawCustomBody(Body: TBody): Boolean; virtual;
        function DrawCustomShape(Shape: TShape): Boolean; virtual;
        function DrawCustomJoint(Joint: TJoint): Boolean; virtual;
        { Draw the physics shapes included in the category mask in studio
          coordinates with the space locked. Drawing happens in its own canvas
          frame, so do not call this between your own canvas frame calls. }
        procedure DrawPhysics(Categories: TBitmask = $FFFFFFFF);
        property Space: TSpace read FSpace;
        property GrabJoint: TJoint read FGrabJoint;
        property GrabBodies: Boolean read FGrabBodies write FGrabBodies;
        property GrabCentered: Boolean read FGrabCentered write FGrabCentered;
      public
        procedure Initialize; override;
        procedure Finalize; override;
        { The scene is stepped every PhysicsStep seconds }
        function StepInterval: Double; override;
        { Step locks the space and calls Simulate unless the scene is paused }
        procedure Step(DeltaTime: Double); override;
        procedure DoMouseDown(var Args: TSceneMouseArgs); override;
        procedure DoMouseMove(var Args: TSceneMouseArgs); override;
        procedure DoMouseUp(var Args: TSceneMouseArgs); override;
        { Free the space and everything in it and create a new empty space. Read the
          Space property again afterwards, as copies of it are no longer valid. }
        procedure ResetSpace;
        { Release the joint used to drag a body. Call this before freeing bodies
          yourself, because freeing a body also frees the joints connected to it. }
        procedure ReleaseGrab;
        { Lock the space to use it outside of the step thread }
        procedure Lock;
        { Unlock the space after Lock }
        procedure Unlock;
        { When paused the space is not stepped }
        property Paused: Boolean read FPaused write FPaused;
      end;

    See also

    Float in Codebot.System.pas
    TBitmask in Codebot.Physics.pas
    TBody in Codebot.Physics.pas
    TJoint in Codebot.Physics.pas
    TSceneMouseArgs in Codebot.Render.Scenes.pas
    TShape in Codebot.Physics.pas
    TSpace in Codebot.Physics.pas
    TVec2 in Codebot.Geometry.pas
    TWidgetScene in Codebot.Render.Scenes.Widgets.pas