SimulationBall
SimulationBall : PVInstance
Overview
Simulation Ball is a Ball object designed to solve the problem of inaccurate synchronization of the physics engine in games. Traditional physics-based balls had issues where each client rendered the ball at different positions due to server latency effects. However, SimulationBall allows all clients to share the same position, velocity, and rotation based on pre-simulated trajectory data.
By using this method, you can obtain the following advantages:
Latency Compensation: Maintains the same ball movement regardless of server-client latency.
High Performance: Improves performance by eliminating frame-by-frame physics calculations.
Predictable Results: Easily query position, velocity, and rotation at a specific point in time based on simulated results.
Complex Physics Implementation: Enables implementation of non-linear movements based on rotation, such as the Magnus effect.
Properties
BallRadius
number
Specifies the radius of the ball. It determines the physical collision and rendering size used in the simulation, and is used to match the ball's size with collision detection in the actual game. Larger values can affect the calculation of the ball's mass and air resistance.
Code Samples
local ball = workspace:FindFirstChild("SimulationBall")
ball.BallRadius = 50BallState
Enum.BallState
Indicates the current state of the Simulation Ball. It can have the following state values:
Simulated(0): The simulation is complete and ready for playback.Playing(1): The simulation is currently playing.Stopped(2): The simulation is stopped.Paused(3): The simulation is paused.
This property is read-only, and the state changes via the Play(), Pause(), and Stop() methods.
Code Samples
CFrame
CFrame
A CFrame representing the current position and rotation of the Simulation Ball. Changing this property immediately updates the ball's position and rotation, and it is automatically updated according to PlaybackTime during simulation.
Code Samples
Color
Color3
Specifies the color of the ball. This property only controls the visual representation of the ball and does not affect physical movement.
Code Samples
EnablePathMarker
bool
Controls the visibility of path markers that visually display the ball's movement trajectory. If set to true, the simulated path of the ball is visually displayed, which is useful for debugging or visualization.
Code Samples
IsPathMarkerWorldSpace
bool
Specifies whether path markers use the world space coordinate system. If set to true, path markers are displayed fixed in world space. If set to false, they are displayed relative to the ball's local space.
Code Samples
Material
Enum.Material
Specifies the surface material of the ball. Visual representation and physical reactions (friction, restitution, etc.) may vary depending on the material.
Code Samples
MaterialVariant
string
A string specifying a variant of the material. Some materials support multiple variants, and this property allows you to select a specific variant.
Code Samples
PathMarkerScale
number
Specifies the size scale of the path markers. Larger values make the path markers appear larger. The default value is 0.2.
Code Samples
SlomoFactor
number
Specifies the multiplier for the simulation playback speed. 1.0 is normal speed, 0.5 is half speed, and 2.0 is double speed. It can be used to implement slow-motion effects or time-lapses.
Code Samples
TextureId
string
The Asset ID of the texture to apply to the ball's surface. This controls only the visual representation and does not affect physical movement. For example, you can express various styles such as soccer balls, basketballs, etc.
Code Samples
Methods
FindNextBallBounce
Returns information about the next bounce (collision) that will occur after the current playback time (PlaybackTime). If there is no bounce or if it is a sliding collision, an empty BallBounce object may be returned.
Parameters
Return
BallBounce
A BallBounce object containing information about the next bounce. If there is no bounce, an empty object is returned.
Code Samples
GetAngularVelocityAtTime
Returns the angular velocity (Vector3) of the ball when a specific time (Time) has elapsed since the simulation started. This value represents the rotation direction and speed of the ball, and can be used for the Magnus effect or rotation-based trajectory prediction.
Parameters
number Time
Time elapsed since the simulation started (in seconds). Queries the ball's angular velocity at the specified point.
Return
Vector3
The angular velocity of the ball at the specified point. Direction represents the axis of rotation, and magnitude represents the angular speed.
Code Samples
GetBallBounceByIndex
Returns bounce information corresponding to the specified index. The index represents the order of bounces that occurred during the simulation, starting from 0. If the index is invalid, an empty BallBounce object may be returned.
Parameters
number bounceIndex
The index of the bounce to query. Starts from 0 and represents the sequence of bounces occurred during the simulation.
Return
BallBounce
A BallBounce object containing bounce information for the specified index.
Code Samples
GetBallSnapshots
Returns an array of all snapshots generated during the simulation. Each snapshot contains the ball's state (position, rotation, velocity, spin, etc.) at a specific point in time, sorted chronologically.
Parameters
Return
array
An array of BallSnapshot objects. Each snapshot represents a specific point in the simulation.
Code Samples
GetBestDirectionToTargetAtTime
Calculates the optimal direction to launch the ball towards a target position at the specified playback time. It returns the direction with the highest probability of reaching the target through physics simulation considering spin.
Parameters
number InPlaybackTime
Simulation playback time. The ball is launched at this point.
Vector3 InTargetPosition
Target position.
number InSpeed
Launch speed.
Vector3 SpinAxis
Rotation axis vector.
number InSpinSpeed
Rotation speed.
number InStepCount
Number of simulation steps.
number InTargetRadius
Target radius. It is considered a success if reached within this range.
number InMaxSampleCount
Maximum number of samples.
Return
Vector3
The optimal direction vector towards the target position.
Code Samples
GetCFrameAtTime
Returns the ball's position and rotation (CFrame) at a specified time (Time) after the simulation starts. This method is useful for obtaining the exact position of the ball at a future or past point in time, and is often used by NPCs or AI to predict the ball's landing spot.
Parameters
number Time
Time elapsed since the simulation started (in seconds). Queries the ball's CFrame at the specified point.
Return
CFrame
The CFrame of the ball at the specified point. You can know the position and rotation values.
Code Samples
GetCurrentPlaybackPosition
Returns the ball's position at the current playback time (PlaybackTime). This method returns the same result as GetCFrameAtTime(ball.PlaybackTime).Position.
Parameters
Return
Vector3
The ball's position at the current playback time.
Code Samples
GetCurrentSnapshotIndex
Returns the index of the snapshot corresponding to the current playback time (PlaybackTime). The snapshot index indicates the position in the snapshot array generated during the simulation.
Parameters
Return
number
The index of the snapshot corresponding to the current playback time.
Code Samples
GetLinearVelocityAtTime
Returns the linear velocity (Vector3) at a specified time (Time) after the simulation starts. It is used when calculating the ball's movement direction and speed, and is utilized for calculating reflection angles upon collision or visualizing the ball's trajectory.
Parameters
number Time
Time elapsed since the simulation started (in seconds). Queries the ball's velocity at the specified point.
Return
Vector3
The ball's velocity at the specified point. Direction represents the movement direction, and magnitude represents the speed.
Code Samples
GetRemainedTimeForNextBounce
Returns the time remaining from the current playback time until the next bounce. If there is no bounce or it is a sliding collision, a very large value (FLT_MAX) is returned.
Parameters
Return
number
Time remaining until the next bounce (in seconds). If there is no bounce, a very large value is returned.
Code Samples
GetSpeedAtTime
Returns the scalar speed at a specified time (Time) after the simulation starts. Unlike GetLinearVelocityAtTime, it does not have direction information and simply provides the magnitude of the velocity.
Parameters
number Time
Time elapsed since the simulation started (in seconds). Queries the ball's speed at the specified point.
Return
number
The ball's speed at the specified point. Unit is cm/s.
Code Samples
IsValidBounceIndex
Checks if the specified index is a valid bounce index. Verifies that the index is within the range of bounces that occurred during the simulation.
Parameters
number bounceIndex
The bounce index to check.
Return
bool
Returns true if the index is valid, false otherwise.
Code Samples
Pause
Pauses the currently running simulation. This method can be resumed with Play(), and the time paused is not reflected in the simulation. Used when implementing game pauses or slow-motion effects.
Parameters
Return
void
No return value.
Code Samples
Play
Plays simulation data to actually execute the ball's movement. It is played with the same timing and results on all clients, and it is also possible to continue playing after Pause().
Parameters
Return
void
No return value.
Code Samples
ReSimulateSpinToTargetWithDelay
{Description Slot}
Parameters
number InDelayTime
Vector3 InTargetPosition
number InSpeed
Vector3 InSpinAxis
number InSpinSpeed
number InStepCount
Return
bool
Code Samples
ReSimulateToTargetWithDelay
Resimulates to head towards the target position after a specified delay time from the current playback time. This method recalculates the trajectory so that the ball heads to the target point at a specific time. Spin is automatically calculated based on the current angular velocity.
Parameters
number InDelayTime
Delay time from the current playback time (in seconds).
Vector3 InTargetPosition
Target position.
number InSpeed
Launch speed.
number InStepCount
Number of simulation steps.
Return
bool
Returns true if resimulation starts successfully, false otherwise.
Code Samples
ReSimulateWithDelay
Resimulates with the specified direction and velocity after a specified delay time from the current playback time. This method recalculates the ball's trajectory with a new direction and velocity at a specific point in time.
Parameters
number InDelayTime
Delay time from the current playback time (in seconds).
Vector3 InDirection
Launch direction vector.
number InSpeed
Launch speed.
Vector3 InSpinAxis
Rotation axis vector.
number InSpinSpeed
Rotation speed.
number InStepCount
Number of simulation steps.
Return
void
No return value.
Code Samples
SetPlaybackTime
Changes the simulation's progress time to an arbitrary point. This allows you to rewind to a specific moment or immediately check the state of a future point for testing purposes. For example, calling SetPlaybackTime(2.5) sets the simulation to a state where 2.5 seconds have progressed.
Parameters
number Time
Target point in time to move to within the simulation (in seconds). Specify a real number value of 0 or greater, where 0 means the simulation start point.
Return
void
No return value.
Code Samples
Simulate
Performs physics simulation of the Simulation Ball and pre-calculates the ball's movement trajectory based on the specified parameters (BallSimParams). This function is used to prepare the trajectory before playing with Play() or to analyze simulation results separately. The result is returned as an array containing information such as position, velocity, rotation, etc., calculated for each point in time.
Parameters
BallSimParams InBallSimParams
A physics parameter structure used for simulation. Contains physical properties such as mass, gravity, initial velocity, spin, damping, and collision properties, and trajectory results vary depending on these values.
Return
array
An array containing simulation results, where each element represents the ball's state (BallSnapshot) sorted chronologically.
Code Samples
Stop
Stops the currently running simulation. Unlike Pause(), it aborts playback, and calling Play() again will replay from the beginning of the simulation.
Parameters
Return
void
No return value.
Code Samples
Events
Bounded
Event called when the Simulation Ball bounces (collision reflection) with another part. Unlike the Touched event, it is called only when a bounce occurs, and sliding collisions are not included.
Parameters
BasePart otherPart
The part that collided with the ball.
Code Samples
Paused
Event called when the simulation is paused. This event occurs when the Pause() method is called.
Parameters
Code Samples
Played
Event called when the simulation starts playing. This event occurs when the Play() method is called.
Parameters
Code Samples
Stopped
Event called when the simulation is stopped. This event occurs when the Stop() method is called.
Parameters
Code Samples
TouchEnded
Event called when the Simulation Ball ends contact with another part. Occurs when the ball moves away after colliding with a part.
Parameters
BasePart otherPart
The part that the ball had collided with.
Code Samples
Touched
Event called when the Simulation Ball collides with another part. You can use this event to implement collision-based logic such as goal detection, reflection handling, sound playback, etc.
Parameters
BasePart otherPart
The part that collided with the ball.
Code Samples
Last updated