修改水
This commit is contained in:
@@ -2,16 +2,17 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Obi{
|
||||
namespace Obi
|
||||
{
|
||||
|
||||
/**
|
||||
/**
|
||||
* Small helper class that lets you specify Obi-only properties for rigidbodies.
|
||||
*/
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class ObiRigidbody2D : ObiRigidbodyBase
|
||||
{
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class ObiRigidbody2D : ObiRigidbodyBase
|
||||
{
|
||||
public Rigidbody2D unityRigidbody { get; private set; }
|
||||
|
||||
public Vector2 position => unityRigidbody.position;
|
||||
@@ -23,26 +24,22 @@ namespace Obi{
|
||||
private Quaternion prevRotation;
|
||||
private Vector3 prevPosition;
|
||||
|
||||
protected override void OnEnable()
|
||||
public override void OnEnable()
|
||||
{
|
||||
unityRigidbody = GetComponent<Rigidbody2D>();
|
||||
ResetPosition();
|
||||
prevPosition = transform.position;
|
||||
prevRotation = transform.rotation;
|
||||
|
||||
linearVelocity = unityRigidbody.linearVelocity;
|
||||
angularVelocity = unityRigidbody.angularVelocity;
|
||||
base.OnEnable();
|
||||
}
|
||||
|
||||
public void ResetPosition()
|
||||
{
|
||||
prevPosition = unityRigidbody.position;
|
||||
prevRotation = Quaternion.AngleAxis(unityRigidbody.rotation, Vector3.forward);
|
||||
linearVelocity = unityRigidbody.linearVelocity;
|
||||
angularVelocity = unityRigidbody.angularVelocity;
|
||||
}
|
||||
|
||||
private void CacheVelocities(float stepTime)
|
||||
private void UpdateKinematicVelocities(float stepTime)
|
||||
{
|
||||
// differentiate positions/orientations to get our own velocites for kinematic objects.
|
||||
// when calling Physics.Simulate, MovePosition/Rotation do not work correctly. Also useful for animations.
|
||||
if (unityRigidbody.isKinematic && stepTime > 0)
|
||||
if (unityRigidbody.isKinematic)
|
||||
{
|
||||
// differentiate positions to obtain linear velocity:
|
||||
linearVelocity = (transform.position - prevPosition) / stepTime;
|
||||
@@ -64,35 +61,27 @@ namespace Obi{
|
||||
|
||||
public override void UpdateIfNeeded(float stepTime)
|
||||
{
|
||||
// Rigidbody might not exist, as rigidbody deletion is buffered.
|
||||
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
|
||||
if (unityRigidbody == null) return;
|
||||
|
||||
CacheVelocities(stepTime);
|
||||
UpdateKinematicVelocities(stepTime);
|
||||
var world = ObiColliderWorld.GetInstance();
|
||||
|
||||
var rb = world.rigidbodies[Handle.index];
|
||||
var rb = world.rigidbodies[handle.index];
|
||||
rb.FromRigidbody(this);
|
||||
world.rigidbodies[Handle.index] = rb;
|
||||
world.rigidbodies[handle.index] = rb;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Reads velocities back from the solver.
|
||||
*/
|
||||
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
|
||||
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
|
||||
{
|
||||
// Rigidbody might not exist, as rigidbody deletion is buffered.
|
||||
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
|
||||
if (unityRigidbody == null) return;
|
||||
|
||||
// kinematic rigidbodies are passed to Obi with zero velocity, so we must ignore the new velocities calculated by the solver:
|
||||
if (Application.isPlaying && !(unityRigidbody.isKinematic || kinematicForParticles))
|
||||
{
|
||||
unityRigidbody.linearVelocity += new Vector2(linearDelta.x, linearDelta.y);
|
||||
unityRigidbody.angularVelocity += angularDelta[2] * Mathf.Rad2Deg;
|
||||
}
|
||||
unityRigidbody.linearVelocity += new Vector2(linearDelta.x, linearDelta.y);
|
||||
unityRigidbody.angularVelocity += angularDelta[2] * Mathf.Rad2Deg;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user