public final class PhysicsWorld extends Object implements EntityWorldListener, ContactListener
Contains several static and instance methods to convert pixels coordinates to meters and vice versa.
Collision handling unifies how they are processed.
Constructor and Description |
---|
PhysicsWorld(int appHeight,
double ppm) |
Modifier and Type | Method and Description |
---|---|
void |
addCollisionHandler(CollisionHandler handler)
Registers a collision handler.
|
void |
beginContact(Contact contact)
Called when two fixtures begin to touch.
|
void |
clear()
Clears collidable entities and active collisions.
|
void |
clearCollisionHandlers() |
void |
endContact(Contact contact)
Called when two fixtures cease to touch.
|
int |
getAppHeight() |
World |
getJBox2DWorld()
Note: certain modifications to the jbox2d world directly may not be
recognized by FXGL.
|
void |
onEntityAdded(Entity entity)
Called after entity was added to the world.
|
void |
onEntityRemoved(Entity entity)
Called after entity was removed from the world
but before entity has been cleaned.
|
void |
onUpdate(double tpf) |
void |
postSolve(Contact contact,
ContactImpulse impulse)
This lets you inspect a contact after the solver is finished.
|
void |
preSolve(Contact contact,
Manifold oldManifold)
This is called after a contact is updated.
|
RaycastResult |
raycast(javafx.geometry.Point2D start,
javafx.geometry.Point2D end)
Performs a ray cast from start point to end point.
|
void |
removeCollisionHandler(CollisionHandler handler)
Removes a collision handler
|
void |
setGravity(double x,
double y)
Set global world gravity.
|
float |
toMeters(double pixels)
Converts pixels to meters
|
float |
toPixels(double meters)
Converts meters to pixels
|
Vec2 |
toPoint(javafx.geometry.Point2D p)
Converts a point in pixel space to a point in physics space.
|
javafx.geometry.Point2D |
toPoint(Vec2 p)
Converts a point in physics space to a point in pixel space.
|
Vec2 |
toVector(javafx.geometry.Point2D v)
Converts a vector of type Point2D to vector of type Vec2
|
javafx.geometry.Point2D |
toVector(Vec2 v)
Converts a vector of type Vec2 to vector of type Point2D
|
public World getJBox2DWorld()
public int getAppHeight()
public void onEntityAdded(Entity entity)
EntityWorldListener
onEntityAdded
in interface EntityWorldListener
entity
- the entitypublic void onEntityRemoved(Entity entity)
EntityWorldListener
onEntityRemoved
in interface EntityWorldListener
entity
- the entitypublic void onUpdate(double tpf)
public void clear()
public void clearCollisionHandlers()
public void beginContact(Contact contact)
ContactListener
beginContact
in interface ContactListener
contact
- contact infopublic void endContact(Contact contact)
ContactListener
endContact
in interface ContactListener
contact
- contact infopublic void preSolve(Contact contact, Manifold oldManifold)
ContactListener
Note:
preSolve
in interface ContactListener
contact
- contact infooldManifold
- the old manifoldpublic void postSolve(Contact contact, ContactImpulse impulse)
ContactListener
Note:
postSolve
in interface ContactListener
contact
- contact infoimpulse
- contact impulsepublic void addCollisionHandler(CollisionHandler handler)
Example: PhysicsWorld physics = ... physics.addCollisionHandler(new CollisionHandler(Type.PLAYER, Type.ENEMY) { public void onCollisionBegin(Entity a, Entity b) { // called when entities start touching } public void onCollision(Entity a, Entity b) { // called when entities are touching } public void onCollisionEnd(Entity a, Entity b) { // called when entities are separated and no longer touching } });
handler
- collision handlerpublic void removeCollisionHandler(CollisionHandler handler)
handler
- collision handler to removepublic void setGravity(double x, double y)
x
- x componenty
- y componentpublic RaycastResult raycast(javafx.geometry.Point2D start, javafx.geometry.Point2D end)
start
- start pointend
- end pointpublic float toMeters(double pixels)
pixels
- value in pixelspublic float toPixels(double meters)
meters
- value in meterspublic Vec2 toVector(javafx.geometry.Point2D v)
v
- vector in pixelspublic javafx.geometry.Point2D toVector(Vec2 v)
v
- vector in meterspublic Vec2 toPoint(javafx.geometry.Point2D p)
p
- point in pixel spacepublic javafx.geometry.Point2D toPoint(Vec2 p)
p
- point in physics spaceCopyright © 2017. All rights reserved.