Class Grabber2DScript
Pick up any dynamic Box2D body with the mouse, drag it about, and let go - with whatever velocity it had, so a flick throws it. Put it on the camera entity and give it the simulation.
[DataContract("Grabber2DScript")]
[Display("Grabber 2D (pick up Box2D bodies with the mouse)", null)]
[ComponentCategory("Physics")]
public class Grabber2DScript : SyncScript, IIdentifiable, ICollectorHolder
- Inheritance
-
Grabber2DScript
- Implements
- Inherited Members
Examples
game.GetCameraEntity().Add(new Grabber2DScript { Simulation = simulation });
Remarks
Box2D v3 has no mouse joint, so this uses the idiom of Box2D's own sample browser: a kinematic anchor body is created at the grab point and a motor joint ties the picked body to it; every frame the anchor is moved to the cursor with a target transform, so it carries a velocity the joint feels; on release both are destroyed. The body is never moved directly, so it keeps colliding and pushing while held.
Tuning follows the samples: a 7.5 Hz spring with critical damping, a force cap of ForceScale times the body's weight so heavy and light bodies drag alike, and an angular friction torque scaled by the body's lever arm so it does not spin freely on the end of the cursor. The grab ends by itself if the body is destroyed under the cursor.
Properties
Button
The mouse button that grabs while held down. Left by default; the 2D camera controller pans with the right one.
public MouseButton Button { get; set; }
Property Value
ForceScale
Force cap as a multiple of the held body's weight, so mass does not change the feel.
public float ForceScale { get; set; }
Property Value
Held
The body being held, or null.
[DataMemberIgnore]
public B2BodyId? Held { get; }
Property Value
- B2BodyId?
PickRadius
Half-size of the box around the cursor that a body must overlap to be picked, in metres.
public float PickRadius { get; set; }
Property Value
Simulation
The simulation whose bodies can be grabbed. Required.
[DataMemberIgnore]
public Box2DSimulation? Simulation { get; set; }
Property Value
SpringDampingRatio
Spring damping ratio of the hold; 1 is critical.
public float SpringDampingRatio { get; set; }
Property Value
SpringHertz
Spring stiffness of the hold, in hertz.
public float SpringHertz { get; set; }
Property Value
Target
Where the held body is being pulled to, in world space.
[DataMemberIgnore]
public Vector2 Target { get; }
Property Value
Methods
Cancel()
public override void Cancel()
Carry(Vector2)
Moves the point the held body is pulled to. The mouse path calls this every frame.
public void Carry(Vector2 worldPoint)
Parameters
worldPointVector2
Grab(B2BodyId, Vector2)
Grabs body at worldPoint. The mouse path calls this
from a pick; a game can call it to hand the player something.
public bool Grab(B2BodyId body, Vector2 worldPoint)
Parameters
bodyB2BodyIdworldPointVector2
Returns
Release()
Lets go of the held body, if any. It keeps its current velocity.
public void Release()
Start()
public override void Start()
Update()
public override void Update()
Events
Grabbed
Raised when a body is picked up.
public event Action<B2BodyId>? Grabbed
Event Type
- Action<B2BodyId>
Released
Raised when the held body is let go, for any reason.
public event Action<B2BodyId>? Released
Event Type
- Action<B2BodyId>