Box2D.FormWithBox2D

From Appmethod Code Examples
Jump to: navigation, search

A simulation that uses FireMonkey controls as objects in a Box2D world.

Location

You can find the FormWithBox2D sample project at:

Description

A simulation that uses FireMonkey controls as objects in a Box2D world.

The application uses the following controls:

  • ToolBar1: Contains the Drop and Reset buttons.
    • ButtonPhysics: Fires the ButtonPhysicsClickHandler.
    • ResetBtn: Fires the ResetBtnClickHandler.
  • The following controls do not perform any function; they are simply converted to Box2D objects:
    • Button4
    • CheckBox1
    • Panel1
    • Label19
    • SpeedButton2
    • SpeedButton3
    • PrograssBar1
    • RadioButton1
    • ScrollBar1
    • ScrollBar2
    • StringComboBox1
    • Text2
    • TextBox1
    • TrackBar6

How to Use the Sample

  1. Navigate to one of the locations given above and open FMSimplePhysics.dproj.
  2. Press F9 or choose Run > Run.
  3. Press Drop.

The simulation starts and the controls fall to the "ground". Press the Drop button again to pause the simulation. Press the Reset button to reset the controls. The starting order and position of the controls is random.

Implementation

uFormFmxPhysics

The uFormFmxPhysics unit uses the uFmxPhysicsDemo unit.

The uFormFmxPhysics unit implements the following event handlers:

  • FormDestroy: Calls the Free method of the uFmxPhysicsDemo unit.
  • ButtonPhysicsClick: Starts/stops the simulation. Calls the Start or the Stop method of the uFmxPhysicsDemo unit.
  • FormResize: Calls ConstructWorld.
  • FormShow: Calls ConstructWorld.
  • ResetBtnClick: Stops the simulation. Randomly places the controls from the list of controls on the form, starting from the top-left position. Calls ConstructWorld.

The uFormFmxPhysics unit also implements the following methods:

  • ConstructWorld: Creates an instance of the FFmxPhysicsDemo class and passes the controls from the list of controls to the AddControl method of the uFmxPhysicsDemo unit.
  • GetControls: Returns a list of controls.

uFmxPhysicsDemo

The uFormFmxPhysics unit uses the uSimulationFmxCtrls unit.

The uFormFmxPhysics unit implements the following methods:

  • Create: Initializes the values for the private variables.
  • Destroy: Stops the simulation and frees some variables.
  • AddControl: Takes a FireMonkey control and turns it into an instance of a Box2D body.
  • AddScreenSize: Takes the current screen size and creates boundaries for the simulation.
  • Start: Sets the flag IsRunning to True

.

  • Stop: Sets the flag IsRunning to False.

The uFormFmxPhysics unit implements the following properties:

  • IsRunning: The flag for the simulation execution. Controls whether an internal timer is enabled or disabled. That timer executes individual steps of the simulation.

uSimulationFmxCtrls

The uSimulationFmxCtrls unit uses the uFlatBox2DSimulation unit.

The uFormFmxPhysics unit implements a class TSimulationFmxCtrls that is a descendant of the TFlatBox2DSimulation class from the uFlatBox2DSimulation unit.

The TSimulationFmxCtrls class implements the following methods:

  • AddBoundaryBox: Defines the boundaries for the simulation.
  • AddDynamicBox: Adds a dynamic body to the simulation.

uFlatBox2DSimulation

The uFlatBox2DSimulation unit uses the uCustomSimulation unit.

The uFormFmxPhysics unit implements a class TFlatBox2DSimulation that is a descendant of the TCustomSimulation class from the uCustomSimulation unit.

The TFlatBox2DSimulation class implements the following methods:

  • Initialize: Defines the value for velocity iterations, position iterations, and gravity. Creates a world.
  • Step: Executes one step of the simulation. The timer handler from the uFormFmxPhysics unit calls this method.
  • Finalize: Destroys the world.

The TFlatBox2DSimulation class implements the following properties:

  • Gravity: The value for the gravity property of the world.
  • World: The world.
  • VelocityIterations: The value for velocity iterations.
  • PositionIterations: The value for position iterations.

uCustomSimulation

The uCustomSimulation unit implements a class TCustomSimulation.

The TCustomSimulation class implements the following methods:

  • DoStep: Calls Step and passes it the value of the time step that the GetRawReferenceTime provides.
  • GetRawReferenceTime: Calculates and returns the time step based on the platform where the application runs.

Uses

See Also

Personal tools