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.
– 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):
Xojo (Windows and OSX compatible)
Let’s make some games!