| Demos | ||||
Test 1 |
Test 2 |
Test 3 |
Test 4 |
Test 5 |
The above links lead off to a few basic physics demos, using the Phys2D library as an engine.
Incorporating Phys2D into JavaFX was done by creating a brand new (experimental) library to augment standard scene graph nodes; making them physics-aware. Then a special kind of scene container was created that would model the physical world simulation and manage the physics of any nodes it contained.
First I created a mixin called PhysicalNode,
acting as a base class for common physics properties added
to scene graph nodes. Then I subclassed each required shape
to create a physical alternative (eg: PhysicalRectangle,
PhysicalCircle, PhysicalImageView...)
Next I created a new Scene subclass with the ability to
handle either standard or physical scene graph nodes, animating them
as necessary as per their properties. The new PhysicalScene
allows things like gravity to be set, and relationships between
its physical child nodes to be defined using joints. It also
provides a JavaFX Script friendly event mechanism for notifying
any PhysicalNodes of collisions.
Where necessary coordinate systems etc. were translated to
fit with current JFX nodes (so PhysicalRectangle
has its origin in the top left corner as per JFX, not at its
centre as Phys2D prefers).
To create a scene using the library, PhysicalNodes
are added to a PhysicalScene just like regular JFX
nodes. Two extra steps are required, however:
- Fixed/springy/angled/etc. joints may be defined to connect nodes, if required.
AUpdate: This will now be kicked off automatically via thestart()function needs to be called on the scene to kick off the physics engine updates.postinitblock of thePhysicalScene, unlessautoStartis set to false.
Apart from that physical nodes are used pretty much like their
regular non-physical counterparts. Updates from the physical
model are automatically fed to the node's layoutX,
layoutY, rotate, etc; and manipulating
a node's fields directly (like setting layoutX)
merely requires a call to the node's updatePhysical()
function to keep the physical model in step.
| Source code | Phys2dFX (JavaFX) library | Phys2d (Java) library | ||
Source code (74k) |
JAR (43k) |
PACK200 (8k) |
JAR (108k) |
PACK200 (33k) |






