Xojo/RealBasic: ABPE, the Physics Engine!

ABPE Physics Engine

ABPE Physics Engine

ABPE is a true 2d Newton’s Physics Engine for Xojo and RealBasic! It is the translation of the latest stable (a045) Actionscript Physics Engine engine written by Alec Cove. It uses GDIPlus on Windows and Quartz 2D on Mac to get nice and fast graphics. It’s not an exact translation as some adaptions needed to be done to let it work in Xojo/Realbasic. (They were Actionscript specific). I also added the possiblity to add a texture to the elements. In the attached demo the car and rotator is skinned.

There may be other engines for Xojo, but this one is very easy to use. The basic idea is you can create classes to form ‘objects’ using Rectangles, Circles and Constraints holding the object together. Additional there are some objects like WheelParticles to help you get started. In just a few lines you add them to the engine and they will interact with each other.

Some Features:
– collision detection (Circle-Circle, Rect-Rect, Circle-Rect and Rect-Circle) between groups and within groups
– particles with elasticity, friction, traction, etc…
– Constraints with stiffness that connect particles
– groups of particles and constraints that can interact with each other
– self-buildable forces (like gravity, push, …)
– vector functions
– …

The car you see in the demo for example looks like this:

Sub Constructor(colC as Color, colE as Color)
  Super.Constructor(false)

  wheelParticleA = new ABPEWheelParticle(140, 10, 14, false, 2, 0.3, 0, 1)
  WheelParticleA.SetImage("Wheel.png")
  Particles.Append(wheelParticleA)

  wheelParticleB = new ABPEWheelParticle(200, 10, 14, false, 2, 0.3, 0, 1)
  WheelParticleB.SetImage("Wheel.png")
  Particles.Append(wheelParticleB)

  dim wheelConnector as new ABPESpringConstraint(wheelParticleA, wheelParticleB,0.5, true, 8, 1, false)
  wheelConnector.SetImage("CarBody.png")
  Constraints.Append(wheelConnector)
End Sub

When all objects are created, you add them to the engine like this:

  ABPEEngine.InitEngine(Canvas1, 0.25)

  dim massLessForces as new ABPEVector(0, 3)
  ABPEEngine.addMasslessForce(massLessForces)

  dim mySurfaces as new Surfaces(colA, colB, colC, colD, colE, colPad)
  ABPEEngine.addGroup(mySurfaces)

  dim myBridge as new Bridge(colB, colC, colD)
  ABPEEngine.addGroup(myBridge)

  dim myCapsule as new Capsule(colC)
  ABPEEngine.addGroup(myCapsule)

  myRotator = new Rotator(colB, colE, colF)
  ABPEEngine.addGroup(myRotator)

  dim mySwingDoor as new SwingDoor(colC)
  ABPEEngine.addGroup(mySwingDoor)

  myCar = new Car(colC, colE)
  ABPEEngine.addGroup(myCar)

  myCar.addCollidable(mySurfaces)
  myCar.addCollidable(myBridge)
  myCar.addCollidable(myRotator)
  myCar.addCollidable(mySwingDoor)
  myCar.addCollidable(myCapsule)

  myCapsule.addCollidable(mySurfaces)
  myCapsule.addCollidable(myBridge)
  myCapsule.addCollidable(myRotator)
  myCapsule.addCollidable(mySwingDoor)

In a timer (set to 18ms), we handle the keys to drive the car, do an engine step and the drawing:

  dim keySpeed as Double = 0.2
  If Keyboard.AsyncKeyDown(&h7B) then
    myCar.SetSpeed(-keySpeed)
  elseif Keyboard.AsyncKeyDown(&h7C) then
    myCar.SetSpeed(keySpeed)
  else
    myCar.SetSpeed(0)
  end if
  ABPEEngine.DoStep
  ABPEEngine.Paint(colBack)
  Canvas1.refresh(false)
  myRotator.RotateByRadian(0.02)

I’ll try to write some help for this later.

Here is a demo of the engine on RB2007R4 for windows. It is the same on Xojo on Windows and OSX.

And here is the full source code. There are 2 versions:

RB2007R4 (Windows compatible only):
http://www.gorgeousapps.com/ABPERB2007R4Windows.zip

Xojo (Windows and OSX compatible)
http://www.gorgeousapps.com/ABPE_Xojo_WindowsAndOSX.zip

Let’s make some games!

Alwaysbusy

Click here to Donation if you like my work

Advertisements

About Alwaysbusy

My name is Alain Bailleul and I'm the Senior Software Architect/Engineer at One-Two. I like to experiment with new technologies, Computer Vision and A.I. My projects are programmed in B4X , Xojo, C#, java, HTML, CSS and JavaScript. View all posts by Alwaysbusy

7 responses to “Xojo/RealBasic: ABPE, the Physics Engine!

  • darkpixel

    Very cool project.

    I added a FPS logger which should work correctly, which outputs the framerate in the title of the window.
    Did that by replacing the timer with a thread. The Run method of the timer is:

    Dim keySpeed as Double = 0.2

    Dim t as Integer = 0
    Dim startTime as Integer = Ticks()

    Dim fps as Integer = 0

    while True

    If Keyboard.AsyncKeyDown(&h7B) then
    myCar.SetSpeed(-keySpeed)
    elseif Keyboard.AsyncKeyDown(&h7C) then
    myCar.SetSpeed(keySpeed)
    else
    myCar.SetSpeed(0)
    end if
    ABPEEngine.DoStep
    ABPEEngine.Paint(colBack)
    Canvas1.refresh(false)
    myRotator.RotateByRadian(0.02)

    if Ticks()>startTime + 60 Then
    startTime = Ticks()
    fps = t
    self.Title = “FPS: ” + Str(fps)

    t = 0
    end if

    t = t + 1
    wend

  • davejdukeve

    How do you get events from collisions?

    • Alwaysbusy

      The engine takes care of the collisions by itself. If you want to raise an event, you could maybe raise the event in the ResolveCollision() sub in the ABPEAbstractParticle class. The p as ABPEABstractParticle param contains the particle it has a collision with.

      Not sure how this would work with a lot of particles.

      Probably better to add a collided property to the Particle. You can then use this property in the timer after the ABPEEngine.DoStep and act accordingly.

  • davejdukeve

    I am trying to delete objects which go out of scope (off page) I am looking at the update method to abstractparticle, Is there a way to delete a partical from this method (itself) I don’t see a destroy method anywhere.

  • Alwaysbusy

    You can use RemoveGroup() from the ABPEEngine to remove a complete group of particles

  • Garry Pettet

    Firstly, thanks for the great effort – Xojo needs a physics engine.

    The demo works OK under Carbon but the FPS drops to an unusable 5-10 FPS (guess) under Cocoa and probably 2-3 FPS when the retina support flags are added.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: