private void Footsteps() { if (string.IsNullOrEmpty(Type.SoundFoot1)) { return; } if (mainBody == null) { return; } if (mainBody.Sleeping) { return; } float rayLength = .7f; leftFoot.onGround = false; rightFoot.onGround = false; MapObjectAttachedHelper Leftfoot = GetFirstAttachedObjectByAlias("leftfoot") as MapObjectAttachedHelper; MapObjectAttachedHelper Rightfoot = GetFirstAttachedObjectByAlias("rightfoot") as MapObjectAttachedHelper; if (Leftfoot == null || Rightfoot == null) { return; } Vec3 pos; Quat rot; Vec3 scl; Vec3 downDirection = mainBody.Rotation * new Vec3(0, 0, -rayLength); //leftfoot { Leftfoot.GetGlobalTransform(out pos, out rot, out scl); Vec3 start = pos - downDirection; Ray ray = new Ray(start, downDirection); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); bool collision = false; Vec3 collisionPos = Vec3.Zero; foreach (RayCastResult result in piercingResult) { if (Array.IndexOf(PhysicsModel.Bodies, result.Shape.Body) != -1) { continue; } collision = true; collisionPos = result.Position; break; } if (collision) { leftFoot.onGround = true; } } //right foot { Rightfoot.GetGlobalTransform(out pos, out rot, out scl); Vec3 start = pos - downDirection; Ray ray = new Ray(start, downDirection); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); bool collision = false; Vec3 collisionPos = Vec3.Zero; foreach (RayCastResult result in piercingResult) { if (Array.IndexOf(PhysicsModel.Bodies, result.Shape.Body) != -1) { continue; } collision = true; collisionPos = result.Position; break; } if (collision) { rightFoot.onGround = true; } } if (rightFoot.onGround == false) { playedright = 0; } if (rightFoot.onGround == true && playedright == 0) { playedright = 1; if (PlayFootSound()) { bduplicatestoperbool = true; } } if (leftFoot.onGround == false) { playedleft = 0; } if (leftFoot.onGround == true && playedleft == 0) { playedleft = 1; if (PlayFootSound()) { bduplicatestoperbool = true; } } if (bduplicatestoperbool) { duplicatestoper += TickDelta; if (duplicatestoper > 0.3f) { bduplicatestoperbool = false; duplicatestoper = 0; } } }
private void TickOnGround() { if (AKJetBody == null) { return; } if (AKJetBody.Sleeping) { return; } float rayLength = .7f; leftWheel.onGround = false; rightWheel.onGround = false; MapObjectAttachedHelper Leftwheel = GetFirstAttachedObjectByAlias("leftwheel") as MapObjectAttachedHelper; MapObjectAttachedHelper Rightwheel = GetFirstAttachedObjectByAlias("rightwheel") as MapObjectAttachedHelper; if (Leftwheel == null || Rightwheel == null) { return; } Vec3 pos; Quat rot; Vec3 scl; Vec3 downDirection = AKJetBody.Rotation * new Vec3(0, 0, -rayLength); //leftwheel { Leftwheel.GetGlobalTransform(out pos, out rot, out scl); Vec3 start = pos - downDirection; Ray ray = new Ray(start, downDirection); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); bool collision = false; Vec3 collisionPos = Vec3.Zero; foreach (RayCastResult result in piercingResult) { if (Array.IndexOf(PhysicsModel.Bodies, result.Shape.Body) != -1) { continue; } collision = true; collisionPos = result.Position; break; } if (collision) { leftWheel.onGround = true; } } //right wheel { Rightwheel.GetGlobalTransform(out pos, out rot, out scl); Vec3 start = pos - downDirection; Ray ray = new Ray(start, downDirection); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); bool collision = false; Vec3 collisionPos = Vec3.Zero; foreach (RayCastResult result in piercingResult) { if (Array.IndexOf(PhysicsModel.Bodies, result.Shape.Body) != -1) { continue; } collision = true; collisionPos = result.Position; break; } if (collision) { rightWheel.onGround = true; } } if (rightWheel.onGround == false) { playedright = 0; } if (rightWheel.onGround == true && playedright == 0) { SoundPlay3D(Type.SoundWheel, .7f, true); playedright = 1; } if (leftWheel.onGround == false) { playedleft = 0; } if (leftWheel.onGround == true && playedleft == 0) { SoundPlay3D(Type.SoundWheel, .7f, true); playedleft = 1; } }
/// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnPostCreate(Boolean)"/>.</summary> protected override void OnPostCreate(bool loaded) { base.OnPostCreate(loaded); SetTurnToPosition(Position + Rotation.GetForward() * 100); CreatePhysicsModel(); Body body = PhysicsModel.CreateBody(); mainBody = body; body.Name = "main"; body.Position = Position; body.Rotation = Rotation; body.Sleepiness = 0; body.LinearVelocity = linearVelocityForSerialization; float length = Type.Height - Type.Radius * 2 - Type.WalkUpHeight; if (length < 0) { length = 2.5f; //Log.Error("Length < 0"); //return; } //Bone headbone = null; MapObjectAttachedHelper Headhelper = GetFirstAttachedObjectByAlias("Head") as MapObjectAttachedHelper; //MapObjectAttachedMesh mainMesh = GetFirstAttachedObjectByAlias("Main") as MapObjectAttachedMesh; //foreach (Bone BN in mainMesh.MeshObject.Mesh.Skeleton.Bones) //{ // if (BN.Name == "ValveBiped_forward") // { // headbone = BN; // break; // } //} ////create Head Sphear //{ // Body head = PhysicsModel.CreateBody(); // head.Name = "HeadBody"; // head.Position = Position + new Vec3(0, 0, 2f); // head.Rotation = Rotation; // body.Sleepiness = 0; // body.LinearVelocity = linearVelocityForSerialization; { SphereShape shape = body.CreateSphereShape(); shape.Radius = 0.2f; shape.ContactGroup = (int)ContactGroup.Dynamic; shape.StaticFriction = 0; shape.DynamicFriction = 0; shape.Hardness = 0; shape.Name = "Head"; if (Headhelper != null) { shape.Position = Headhelper.PositionOffset; } //if (headbone != null) //{ // shape.Position = headbone.Position; //} } //} //create main capsule { CapsuleShape shape = body.CreateCapsuleShape(); shape.Length = length; shape.Radius = Type.Radius; shape.ContactGroup = (int)ContactGroup.Dynamic; shape.StaticFriction = 0; shape.DynamicFriction = 0; //shape.Bounciness = 0; shape.Hardness = 0; float r = shape.Radius; shape.Density = Type.Mass / (MathFunctions.PI * r * r * shape.Length + (4.0f / 3.0f) * MathFunctions.PI * r * r * r); shape.SpecialLiquidDensity = .5f; shape.Name = "main"; } //create down capsule { CapsuleShape shape = body.CreateCapsuleShape(); shape.Length = Type.Height - Type.BottomRadius * 2; shape.Radius = Type.BottomRadius; shape.Position = new Vec3(0, 0, (Type.Height - Type.WalkUpHeight) / 2 - Type.Height / 2); shape.ContactGroup = (int)ContactGroup.Dynamic; //shape.Bounciness = 0; shape.Hardness = 0; shape.Density = 0; shape.SpecialLiquidDensity = .5f; shape.Name = "down"; shape.StaticFriction = 0; shape.DynamicFriction = 0; } PhysicsModel.PushToWorld(); AddTimer(); }