CharacterVirtual

Runtime character object. This object usually represents the player. Contrary to the Character class it doesn't use a rigid body but moves doing collision checks only (hence the name virtual). The advantage of this is that you can determine when the character moves in the frame (usually this has to happen at a very particular point in the frame) but the downside is that other objects don't see this virtual character. To make a CharacterVirtual visible to the simulation, you can optionally create an inner rigid body through CharacterVirtualSettings::mInnerBodyShape. A CharacterVirtual is not tracked by the PhysicsSystem so you need to update it yourself. This also means that a call to PhysicsSystem::SaveState will not save its state, you need to call CharacterVirtual::SaveState yourself.

C++ JPH::CharacterVirtual.

Constructors

Link copied to clipboard
constructor(settings: CharacterVirtualSettings?, position: Vec3, rotation: Quat, userData: Long, system: PhysicsSystem?)
constructor(settings: CharacterVirtualSettings?, position: Vec3, rotation: Quat, system: PhysicsSystem?)

Types

Link copied to clipboard

Settings struct with settings for ExtendedUpdate

Properties

Link copied to clipboard

Access to the internal list of contacts that the character has found. Note that only contacts that have their mHadCollision flag set are actual contacts.

Link copied to clipboard
Link copied to clipboard

Calculates the transform for this character's center of mass

Link copied to clipboard

Character padding

Link copied to clipboard

Get the character settings that can recreate this character

Link copied to clipboard
Link copied to clipboard

Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with internal edges of a mesh). This is more expensive but makes bodies move smoother over a mesh with convex edges.

Link copied to clipboard

BodyID of the object the character is standing on. Note may have been removed!

Link copied to clipboard

Material that the character is standing on

Link copied to clipboard

Get the contact normal with the ground

Link copied to clipboard

Get the contact point with the ground

Link copied to clipboard

Current ground state

Link copied to clipboard

Sub part of the body that we're standing on.

Link copied to clipboard

User data value of the body that we're standing on

Link copied to clipboard

Velocity in world space of ground

Link copied to clipboard

Cos(angle) where angle is the maximum angle between two hits contact normals that are allowed to be merged during hit reduction. Default is around 2.5 degrees. Set to -1 to turn off.

Link copied to clipboard

The ID of this character

Link copied to clipboard

Optional inner rigid body that proxies the character in the world. Can be used to update body properties.

Link copied to clipboard

Returns true if the player is supported by normal or steep ground

Link copied to clipboard

Get the linear velocity of the character (m / s)

Link copied to clipboard

Get the current contact listener

Link copied to clipboard
var mass: Float

Character mass (kg)

Link copied to clipboard

Returns if we exceeded the maximum number of hits during the last collision check and had to discard hits based on distance. This can be used to find areas that have too complex geometry for the character to navigate properly. To solve you can either increase the max number of hits or simplify the geometry. Note that the character simulation will try to do its best to select the most relevant contacts to avoid the character from getting stuck.

Link copied to clipboard

Max num hits to collect in order to avoid excess of contact points collection

Link copied to clipboard

Maximum force with which the character can push other bodies (N)

Link copied to clipboard

This value governs how fast a penetration will be resolved, 0 = nothing is resolved, 1 = everything in one update

Link copied to clipboard

Get the position of the character

Link copied to clipboard

Get the rotation of the character

Link copied to clipboard
val shape: Shape?

Get the current shape that the character is using.

Link copied to clipboard

An extra offset applied to the shape in local space. This allows applying an extra offset to the shape in local space. Note that setting it on the fly can cause the shape to teleport into collision.

Link copied to clipboard
Link copied to clipboard

Get the transformed shape that represents the volume of the character, can be used for collision checks.

Link copied to clipboard
var up: Vec3

Set the up vector for the character

Link copied to clipboard

Access to the user data, can be used for anything by the application

Link copied to clipboard

Calculate the world transform of the character

Functions

Link copied to clipboard
open override fun addRef()
Link copied to clipboard

This function can be called prior to calling Update() to convert a desired velocity into a velocity that won't make the character move further onto steep slopes. This velocity can then be set on the character using SetLinearVelocity()

Link copied to clipboard
fun canWalkStairs(linearVelocity: Vec3): Boolean

This function will return true if the character has moved into a slope that is too steep (e.g. a vertical wall). You would call WalkStairs to attempt to step up stairs.

Link copied to clipboard
fun checkCollision(position: Vec3, rotation: Quat, movementDirection: Vec3, maxSeparationDistance: Float, shape: Shape?, baseOffset: Vec3, collector: CollideShapeCollector, broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter)

Get all contacts for the character at a particular location. When colliding with another character virtual, this pointer will be provided through CollideShapeCollector::SetUserContext before adding a hit.

Link copied to clipboard
open override fun close()
Link copied to clipboard
fun extendedUpdate(deltaTime: Float, gravity: Vec3, settings: CharacterVirtual.ExtendedUpdateSettings, broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter, allocator: TempAllocator)

This function combines Update, StickToFloor and WalkStairs. This function serves as an example of how these functions could be combined. Before calling, call SetLinearVelocity to update the horizontal/vertical speed of the character, typically this is:

Link copied to clipboard

This call triggers contact removal callbacks and is used in conjunction with StartTrackingContactChanges.

Link copied to clipboard

Check if the character is currently in contact with or has collided with another body in the last operation (e.g. Update or WalkStairs)

Check if the character is currently in contact with or has collided with another character in the last time step (e.g. Update or WalkStairs)

Link copied to clipboard

Check if the normal of the ground surface is too steep to walk on

Link copied to clipboard
fun refreshContacts(broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter, allocator: TempAllocator)

This function can be used after a character has teleported to determine the new contacts with the world.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Set the character vs character collision interface

Link copied to clipboard

Updates the shape of the inner rigid body. Should be called after a successful call to SetShape.

Link copied to clipboard
fun setMaxSlopeAngle(maxSlopeAngle: Float)

Set the maximum angle of slope that character can still walk on (radians)

Link copied to clipboard
fun setShape(shape: Shape?, maxPenetrationDepth: Float, broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter, allocator: TempAllocator): Boolean

Switch the shape of the character (e.g. for stance).

Link copied to clipboard

This function is internally called by Update, WalkStairs, StickToFloor and ExtendedUpdate and is responsible for tracking if contacts are added, persisted or removed. If you want to do multiple operations on a character (e.g. first Update then WalkStairs), you can surround the code with a StartTrackingContactChanges and FinishTrackingContactChanges pair to only receive a single callback per contact on the CharacterContactListener. If you don't do this then you could for example receive a contact added callback during the Update and a contact persisted callback during WalkStairs.

Link copied to clipboard
fun stickToFloor(stepDown: Vec3, broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter, allocator: TempAllocator): Boolean

This function can be used to artificially keep the character to the floor. Normally when a character is on a small step and starts moving horizontally, the character will lose contact with the floor because the initial vertical velocity is zero while the horizontal velocity is quite high. To prevent the character from losing contact with the floor, we do an additional collision check downwards and if we find the floor within a certain distance, we project the character onto the floor.

Link copied to clipboard
fun update(deltaTime: Float, gravity: Vec3, broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter, allocator: TempAllocator)

This is the main update function. It moves the character according to its current velocity (the character is similar to a kinematic body in the sense that you set the velocity and the character will follow unless collision is blocking the way). Note it's your own responsibility to apply gravity to the character velocity! Different surface materials (like ice) can be emulated by getting the ground material and adjusting the velocity and/or the max slope angle accordingly every frame.

Link copied to clipboard

Use the ground body ID to get an updated estimate of the ground velocity. This function can be used if the ground body has moved / changed velocity and you want a new estimate of the ground velocity. It will not perform collision detection, so is less accurate than RefreshContacts but a lot faster.

Link copied to clipboard
fun walkStairs(deltaTime: Float, stepUp: Vec3, stepForward: Vec3, stepForwardTest: Vec3, stepDownExtra: Vec3, broadPhaseLayerFilter: BroadPhaseLayerFilter, objectLayerFilter: ObjectLayerFilter, bodyFilter: BodyFilter, shapeFilter: ShapeFilter, allocator: TempAllocator): Boolean

When stair walking is needed, you can call the WalkStairs function to cast up, forward and down again to try to find a valid position