<?xml version="1.0" ?>
<ComponentSchema packageName="com.example.physics">
<Component name="CustomPhysics">
<Vector3Attribute
name="initialLinearVelocity"
defaultValue="0.0f, 0.0f, 0.0f"
/>
</Component>
</ComponentSchema>
class CustomPhysicsSystem() : SystemBase() {
private val physicsObjects_ = HashMap<Entity, ScenePhysicsObject>()
override fun execute() {
val q = Query.where { changed(CustomPhysics.id) and has(Transform.id) }
for (ent in q.eval()) {
if (physicsObjects_.containsKey(ent)) {
continue
}
val physics = ScenePhysicsObject.createBox(getScene(), ent, 0.25f, 0.25f, 0.25f, 0.5f)
physicsObjects_.put(ent, physics)
physics.setPose(ent.getComponent<Transform>().transform)
physics.setLinearVelocity(ent.getComponent<CustomPhysics>().initialLinearVelocity)
Log.i("Physics", "Created physics object for entity $ent $physics")
}
}
override fun delete(entity: Entity) {
if (physicsObjects_.containsKey(entity)) {
physicsObjects_[entity]?.destroy()
physicsObjects_.remove(entity)
}
}
open class ScenePhysicsObject
entity
: Entity?
[Get] |
Signature
val entity: Entity? |
handle
: Long
[Get] |
Native handle to the physics object.
Signature
var handle: Long |
applyForce
(
force
)
|
Applies a force to this physics object for the next physics step only. Call it every frame to keep pushing. For a one-off push, set Physics.applyForce, which applies an impulse.
Signature
fun applyForce(force: Vector3) Parameters |
applyForceAtRelativePosition
(
force
, relativePos
)
|
Applies a force to this physics object at a specified position relative to its center, for the next physics step only.
This can create both linear and angular acceleration depending on the force and position.
Signature
fun applyForceAtRelativePosition(force: Vector3, relativePos: Vector3) |
applyTorque
(
torqueX
, torqueY
, torqueZ
)
|
Applies a torque (rotational force) to this physics object.
Signature
fun applyTorque(torqueX: Float, torqueY: Float, torqueZ: Float) Parameters torqueX: Float
The X component of the torque vector
torqueY: Float
The Y component of the torque vector
torqueZ: Float
The Z component of the torque vector
|
destroy
()
|
Signature
fun destroy() |
getPose
()
|
Gets the current position and rotation of this physics object.
Signature
fun getPose(): Pose |
setAllowSleeping
(
allow
)
|
Sets whether this body may fall asleep. A sleeping body costs no CPU and stays still until something touches or moves it; PhysicsFeature.setSleepSettings decides when bodies fall asleep. A body that may not sleep keeps moving, however slowly.
For an entity with a Physics component, set its allowSleeping instead. The component's value replaces this whenever the component changes.
Signature
fun setAllowSleeping(allow: Boolean) Parameters allow: Boolean
Whether the body may fall asleep. Bodies may by default.
|
setAngularVelocity
(
angularVelocity
)
|
Sets the angular velocity of this physics object.
Signature
fun setAngularVelocity(angularVelocity: Vector3) Parameters |
setBodyType
(
bodyType
, mass
)
|
Sets the body type (STATIC, DYNAMIC, or KINEMATIC) of this physics object.
Signature
fun setBodyType(bodyType: PhysicsState, mass: Float) Parameters mass: Float
The mass for dynamic bodies, in kg (ignored for static/kinematic). A dynamic body needs a mass above 0; with none, it stays static.
|
setContinuousCollision
(
enabled
)
|
Sets whether this body uses continuous collision detection. With it on, a body that moves far in one physics step is swept along its path, so small, fast objects such as a thrown ball stop at thin colliders instead of passing through them. The sweep only runs while the body moves fast, so slow bodies cost nothing extra. A fast body can appear to pause for one step when it hits something. A body whose shape has no thickness, such as a flat disc, keeps discrete collision detection.
Signature
fun setContinuousCollision(enabled: Boolean) Parameters enabled: Boolean
Whether to use continuous collision detection. It is off by default.
|
setDamping
(
linearDamping
, angularDamping
)
|
Sets the linear and angular damping for this physics object.
Damping reduces velocity over time, simulating air resistance or friction. Each value is a rate per second: with damping d, velocity decays by about e^(-d * t) after t seconds, so 0 means no damping and higher values slow the body faster.
Bodies start with 0.05 for both. Nothing else slows a rolling body, so at that value thrown objects keep spinning and balls roll a long way. Raise angularDamping to make them settle sooner.
For an entity with a Physics component, set its linearDamping and angularDamping instead. The component's values replace these whenever the component changes.
Signature
fun setDamping(linearDamping: Float = 0.05f, angularDamping: Float = 0.05f) Parameters linearDamping: Float
Linear velocity damping rate. Default: 0.05
angularDamping: Float
Angular velocity damping rate. Default: 0.05
|
setDeactivationTime
(
time
)
DeprecatedHas no effect. Use PhysicsFeature.setSleepSettings for the world's sleep settings, or setAllowSleeping to keep this body awake. |
Has no effect. The physics engine puts a body to sleep once it has been nearly still for half a second, and wakes it when something touches or moves it. Sleep settings apply to the whole world: use PhysicsFeature.setSleepSettings to change them, or ScenePhysicsObject.setAllowSleeping to keep this body awake.
Signature
fun setDeactivationTime(time: Float = 0.8f) Parameters time: Float
Ignored.
|
setFriction
(
friction
, rolling
, spinning
)
|
Sets the friction properties of this physics object.
A contact uses the geometric mean of the two bodies' surface friction coefficients.
Signature
fun setFriction(friction: Float, rolling: Float = friction, spinning: Float) Parameters friction: Float
The surface friction coefficient
rolling: Float
Has no effect. The physics engine has no rolling friction; use ScenePhysicsObject.setDamping to slow rolling bodies.
spinning: Float
Has no effect. The physics engine has no spinning friction.
|
setLinearVelocity
(
velocity
)
|
Sets the linear velocity of this physics object.
Signature
fun setLinearVelocity(velocity: Vector3) Parameters |
setPose
(
pose
)
|
Sets the position and rotation of this physics object.
Signature
fun setPose(pose: Pose) Parameters |
setRestitution
(
restitution
)
|
Sets the restitution (bounciness) of this physics object.
Signature
fun setRestitution(restitution: Float) Parameters restitution: Float
The restitution value (0.0 = no bounce)
|
setSleepingThresholds
(
linear
, angular
)
DeprecatedHas no effect. Use PhysicsFeature.setSleepSettings for the world's sleep settings, or setAllowSleeping to keep this body awake. |
Has no effect. The physics engine puts a body to sleep once it has been nearly still for half a second, and wakes it when something touches or moves it. Sleep settings apply to the whole world: use PhysicsFeature.setSleepSettings to change them, or ScenePhysicsObject.setAllowSleeping to keep this body awake.
Signature
fun setSleepingThresholds(linear: Float = 1.6f, angular: Float = 2.5f) Parameters linear: Float
Ignored.
angular: Float
Ignored.
|
createBox
(
scene
, entity
, width
, height
, depth
, mass
)
|
Creates a box-shaped physics object.
Signature
fun createBox(
scene: Scene,
entity: Entity?,
width: Float,
height: Float,
depth: Float,
mass: Float,
): ScenePhysicsObjectParameters width: Float
Half the width of the box in meters
height: Float
Half the height of the box in meters
depth: Float
Half the depth of the box in meters
mass: Float
Mass of the box in kilograms (0 for static objects)
|
createFromCollider
(
scene
, entity
, collider
, mass
)
|
Creates a physics object using an existing collider.
This allows multiple physics objects to share the same collision shape, reducing memory usage when many objects have the same geometry.
Signature
fun createFromCollider(
scene: Scene,
entity: Entity?,
collider: ScenePhysicsCollider,
mass: Float,
): ScenePhysicsObjectParameters mass: Float
Mass of the object in kilograms (0 for static objects)
|
createGLTF
(
scene
, entity
, filename
, mass
)
|
Creates a physics object from a GLTF model file.
The collision shape will be generated from the model's geometry.
Signature
fun createGLTF(scene: Scene, entity: Entity?, filename: String, mass: Float): ScenePhysicsObject Parameters |
createSphere
(
scene
, entity
, radius
, mass
)
|
Creates a sphere-shaped physics object.
Signature
fun createSphere(scene: Scene, entity: Entity?, radius: Float, mass: Float): ScenePhysicsObject Parameters |
setCallbackEntities
(
scene
, entities
)
|
Sets the entities that will receive physics callbacks.
Signature
fun setCallbackEntities(scene: Long, entities: LongArray) Parameters scene: Long
The scene handle
entities: LongArray
Array of entity IDs that will receive physics callbacks
|