本文整理汇总了C#中Body.AddForce方法的典型用法代码示例。如果您正苦于以下问题:C# Body.AddForce方法的具体用法?C# Body.AddForce怎么用?C# Body.AddForce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Body
的用法示例。
在下文中一共展示了Body.AddForce方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyForce
public void ApplyForce(Body body, float TickDelta)
{
Vec3 originalBoneDerivedPosition = bone.Parent.Position + bone.Parent.Rotation * originalBonePosition;
Quat originalBoneDerivedRotation = bone.Parent.Rotation * originalBoneRotation;
Vec3 pos = body.Position + body.Rotation * originalBoneDerivedPosition;
Vec3 dir = body.Rotation * originalBoneDerivedRotation * new Vec3(size, 0, 0);
Ray ray = new Ray(pos, dir);
RayCastResult res = PhysicsWorld.Instance.RayCast(ray, (int)ContactGroup.CastOnlyContact);
if (res.Distance != 0)
{
// Stiffness
float forceStiffness = 0, forceDamping = 0;
forceStiffness = (size - res.Distance) * stiffness;
// Damping
if (lastPos != -1.0f)
{
float speed = res.Distance - lastPos;
forceDamping = -speed * damping;
}
lastPos = res.Distance;
//EngineConsole.Instance.Print("force(" + bone.Name + ")=" + forceStiffness.ToString() + ", " + forceDamping.ToString());
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(0, 0, forceStiffness + forceDamping), originalBoneDerivedPosition);
// Lateral Friction
Vec3 speedAtWheel = body.LinearVelocity + Vec3.Cross(body.AngularVelocity, body.Rotation * bone.GetDerivedPosition());
float lateralForce = -Vec3.Dot(speedAtWheel, body.Rotation * Vec3.XAxis) * 0.3f;
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(lateralForce, 0, 0), bone.GetDerivedPosition());
// Longitudinal Friction
float longitudinalForce = -Vec3.Dot(speedAtWheel, body.Rotation * Vec3.YAxis) * 0.001f;
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(0, longitudinalForce, 0), bone.GetDerivedPosition());
bone.Position = originalBonePosition - new Vec3(0, 0, res.Distance);
float wheelRadius = 0.0439f;
mesh.PositionOffset = originalBoneDerivedPosition - new Vec3(0, 0, res.Distance - wheelRadius);
}
}
示例2: ForceCallback
public void ForceCallback(Body body, float timestep, int threadindex)
{
var data = body.UserData as PhysicsControlData;
if (data != null)
{
body.AddForce(data.Force * body.Mass);
data.Force = Vector3.ZERO;
body.AddTorque(data.Torque * body.Mass);
data.Torque = Vector3.ZERO;
}
if (body.Position.y < -10) body.AddBouyancyForce(1030, 0.0020F, 0.0020F, Vector3.NEGATIVE_UNIT_Y * 9.8f, Plane );
}
示例3: ApplyForceToBody
/// <summary>
/// This will whack the body by the explosion.
/// NOTE: Only call from within the Body.ApplyForceAndTorque callback
/// </summary>
public void ApplyForceToBody(Body body)
{
//TODO: This needs to add some rotation. Otherwise it just looks odd (everything is too even)
Vector3D explosiveForce = GetForceAtPoint(body.Position);
if (explosiveForce.X != 0d || explosiveForce.Y != 0d || explosiveForce.Z != 0d)
{
body.AddForce(explosiveForce);
}
}
示例4: FixPositionsAndVelocities_ORIG
private void FixPositionsAndVelocities_ORIG(Body body)
{
const double RETURNVELOCITY = 0d;
double ZACCEL = 10;
// Get the center of mass in world coords
Point3D position = body.Position;
if (_boundryMin != null) // if min is non null, _boundryMax is also non null. I don't want to waste the processor checking that each frame
{
#region Stay inside bounding box
// Set the velocity going away to zero, apply a force
//NOTE: Position is the center of mass, so if the position is past the boundry, that means the body was travelling too fast for newton to bounce
//it back (there is a wall at the boundry position). So this logic is an extra helper
Vector3D velocity = body.Velocity; // already in world coords
bool modifiedVelocity = false;
#region X
if (position.X < _boundryMin.Value.X)
{
if (velocity.X < 0)
{
velocity.X = RETURNVELOCITY;
modifiedVelocity = true;
}
body.AddForce(new Vector3D(body.Mass * ZACCEL, 0, 0)); // Apply a constant acceleration until it hits zero
}
else if (position.X > _boundryMax.Value.X)
{
if (velocity.X > 0)
{
velocity.X = -RETURNVELOCITY;
modifiedVelocity = true;
}
body.AddForce(new Vector3D(body.Mass * ZACCEL * -1, 0, 0)); // Apply a constant acceleration until it hits zero
}
#endregion
#region Y
if (position.Y < _boundryMin.Value.Y)
{
if (velocity.Y < 0)
{
velocity.Y = RETURNVELOCITY;
modifiedVelocity = true;
}
body.AddForce(new Vector3D(0, body.Mass * ZACCEL, 0)); // Apply a constant acceleration until it hits zero
}
else if (position.Y > _boundryMax.Value.Y)
{
if (velocity.Y > 0)
{
velocity.Y = -RETURNVELOCITY;
modifiedVelocity = true;
}
body.AddForce(new Vector3D(0, body.Mass * ZACCEL * -1, 0)); // Apply a constant acceleration until it hits zero
}
#endregion
#region Z
if (position.Z < _boundryMin.Value.Z)
{
if (velocity.Z < 0)
{
velocity.Z = RETURNVELOCITY;
modifiedVelocity = true;
}
body.AddForce(new Vector3D(0, 0, body.Mass * ZACCEL)); // Apply a constant acceleration until it hits zero
}
else if (position.Z > _boundryMax.Value.Z)
{
if (velocity.Z > 0)
{
velocity.Z = -RETURNVELOCITY;
modifiedVelocity = true;
}
body.AddForce(new Vector3D(0, 0, body.Mass * ZACCEL * -1)); // Apply a constant acceleration until it hits zero
}
#endregion
if (modifiedVelocity)
{
body.Velocity = velocity; // already in world coords
}
#endregion
}
//.........这里部分代码省略.........
示例5: DoForce
private void DoForce(Body otherbody, Body thisbody)
{
float velocity = 0;
//if(MaxVelocity <= Velocity)
//if(MaxVelocity <= Velocity) //)this.GroundRelativeVelocity.Length())
//{
velocity = this.GroundRelativeVelocity.Length() + 1000f; //(speedpersecpersec)
//}
//else
//{
// if (velocity >= MaxVelocity)
// velocity = MaxVelocity;
// else if (Velocity < 1)
// {
// velocity = 0f;
// }
//}
Vec3 vector = otherbody.Position - thisbody.Position;
//vector.Z += 3f;
vector.Normalize();
otherbody.AddForce(ForceType.GlobalAtLocalPos, TickDelta, vector * (velocity * 2), otherbody.Position);
//thisbody.AddForce(ForceType.GlobalAtGlobalPos, TickDelta, -vector * velocity, thisbody.Position);
}
示例6: OnTick
protected override void OnTick()
{
base.OnTick();
if (firstWay == true)
{
ActivateWayMovement();
}
UpdateGeneralTask();
CalculateThings();
chassisBody = PhysicsModel.GetBody("main");
if (chassisBody == null)
{
Log.Error("Emrah Helli: \"main\" chassisBody dose not exists.");
return;
}
float diff = Position.Z - flyHeight;
float force = -diff - chassisBody.LinearVelocity.Z;
MathFunctions.Clamp(ref force, -10, 10);
chassisBody.AddForce(ForceType.GlobalAtLocalPos, TickDelta,
new Vec3(0, 0, force * 3) * chassisBody.Mass, new Vec3(0, 0, 1));
chassisBody.AngularDamping = 1;
this.flyHeight = TaskPosition.Z;
float ZA = ZADiffAngle * 4;
if (ZA < 20 && ZA > -20)
{
chassisBody.AngularVelocity = new Vec3
(chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, ZA);
}
else if (ZA > 20)
{
chassisBody.AngularVelocity = new Vec3
(chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, -2);
}
else if (ZA < -20)
{
chassisBody.AngularVelocity = new Vec3
(chassisBody.AngularVelocity.X, chassisBody.AngularVelocity.Y, 2);
}
chassisBody.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(Velocity * chassisBody.Mass, 0, 0), new Vec3(-1, 0, 0));
chassisBody.LinearDamping = 1;
//check outside Map position
Bounds checkBounds = Map.Instance.InitialCollisionBounds;
checkBounds.Expand(new Vec3(300, 300, 10000));
if (!checkBounds.IsContainsPoint(Position))
SetDeleted();
}
示例7: TickAndApplyForces
public void TickAndApplyForces(Body body, float TickDelta)
{
//mainBody.AddForce(ForceType.LocalAtLocalPos, TickDelta, new Vec3(0, 0, intellect.controlThrust.Value), new Vec3(0, 0, 0));
//PhysicsWorld.Instance.CreateFixedJoint(body,)
/*body.LinearVelocity = new Vec3(0, 10, -4);
body.AngularVelocity = Vec3.Zero;
body.Rotation = Quat.Identity;*/
Vec3 speed = body.LinearVelocity;
Vec3 omega = body.AngularVelocity;
// Apply Thrust
//EngineConsole.Instance.Print("speed = " + speed.ToString());
EngineConsole.Instance.Print("thrustControl = " + awesomeAircraft.controlThrust.Value.ToString());
float airspeed_scale = 1.0f - speed.Length() / handling.speedMax;
float thrustPower = awesomeAircraft.controlThrust.Value * handling.accelMax * airspeed_scale;
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, thrustPower * Vec3.YAxis, new Vec3(0, 5, 0));
if (Utils.LogOn)
totalTime += 0.02f;
//Utils.Write(totalTime.ToString() + "," + thrustPower.ToString() + ",,");
//Utils.Write(Utils.VectToString(body.Position-Utils.LogPosInitial) + ",," + body.Rotation.ToString() + ",,");
//Utils.Write(Utils.VectToString(speed) + ",," + Utils.VectToString(omega) + ",,");
int i = 0;
foreach (Aerofoil aerofoil in aerofoils)
{
Vec3 speedAtAerofoil = speed + Vec3.Cross(omega, body.Rotation * aerofoil.position);
float density = 5;
Vec3 liftL, dragL, forceappL, pitch_forceL, pitch_forceappL;
Mat3 M3Inv = body.Rotation.ToMat3();
M3Inv.Inverse();
aerofoil.get_lift_and_drag(
this,
-M3Inv * speedAtAerofoil,
out liftL, out dragL, out forceappL, out pitch_forceL, out pitch_forceappL,
density,
i,
awesomeAircraft.axisAileron.Value,
awesomeAircraft.axisElevator.Value,
awesomeAircraft.axisRudder.Value
);
float seuil = 100;
if (liftL.Length() > param.CapForce) liftL *= param.CapForce / liftL.Length();
if (dragL.Length() > param.CapForce) dragL *= param.CapForce / dragL.Length();
if (pitch_forceL.Length() > param.CapForce) pitch_forceL *= param.CapForce / pitch_forceL.Length();
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, liftL, forceappL);
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, dragL, forceappL);
body.AddForce(ForceType.LocalAtLocalPos, TickDelta, pitch_forceL, pitch_forceappL);
/* Utils.Write(
Utils.VectToString(forceappL) + ",," +
Utils.VectToString(liftL) + ",," +
Utils.VectToString(dragL) + ",," +
Utils.VectToString(pitch_forceappL) + ",," +
Utils.VectToString(pitch_forceL) + ",,"
);*/
Utils.Write(liftL.Length().ToString() + ',' + dragL.Length().ToString() + ',' + pitch_forceL.Length().ToString() + ",,");
i++;
}
Utils.WriteLine("");
}