public void OldUpdate() { float FrameDelta = Time.fixedDeltaTime; // todo put a variable in the physics world for this. BulletSharp.Math.Matrix xform = m_collisionObject.WorldTransform; BulletSharp.Math.Vector3 forwardDir = new BulletSharp.Math.Vector3(xform.M31, xform.M32, xform.M33); BulletSharp.Math.Vector3 upDir = new BulletSharp.Math.Vector3(xform.M21, xform.M22, xform.M23); forwardDir.Normalize(); upDir.Normalize(); BulletSharp.Math.Vector3 pos = xform.Origin; BulletSharp.Math.Vector3 walkDirection = BulletSharp.Math.Vector3.Zero; const float walkVelocity = 1.1f * 4.0f; float walkSpeed = walkVelocity * FrameDelta * 10;// * 0.0001f; float turnSpeed = FrameDelta * 3; //TODO should not be grabbing input here, control should happen elsewhere if (Input.GetKey(KeyCode.LeftArrow)) { BulletSharp.Math.Matrix orn = xform; orn.Row4 = new BulletSharp.Math.Vector4(0, 0, 0, 1); orn *= BulletSharp.Math.Matrix.RotationAxis(upDir, -turnSpeed); orn.Row4 = new BulletSharp.Math.Vector4(pos.X, pos.Y, pos.Z, 1); m_collisionObject.WorldTransform = orn; } if (Input.GetKey(KeyCode.RightArrow)) { BulletSharp.Math.Matrix orn = xform; orn.Row4 = new BulletSharp.Math.Vector4(0, 0, 0, 1); orn *= BulletSharp.Math.Matrix.RotationAxis(upDir, turnSpeed); orn.Row4 = new BulletSharp.Math.Vector4(pos.X, pos.Y, pos.Z, 1); m_collisionObject.WorldTransform = orn; } if (Input.GetKey(KeyCode.UpArrow)) { walkDirection += forwardDir; } if (Input.GetKey(KeyCode.DownArrow)) { walkDirection -= forwardDir; } m_characterController.SetWalkDirection(walkDirection * walkSpeed); if (Input.GetKey(KeyCode.Space)) { m_characterController.Jump(); return; } //TODO fixme BulletSharp.Math.Matrix trans; m_collisionObject.GetWorldTransform(out trans); transform.position = trans.Origin.ToUnity(); }
CollisionShape _AddFlipperCylinders(FlipperBehavior flipper) { float r1 = flipper.data.BaseRadius; float r2 = flipper.data.EndRadius; float h = flipper.data.Height; float l = flipper.data.FlipperRadius; var hh = h * 0.5f; // half height var cs = new BulletSharp.CompoundShape(); cs.AddChildShape( Matrix.Translation(0, 0, hh), new CylinderShapeZ(r1, r1, hh)); cs.AddChildShape( Matrix.Translation(0, -l, hh), new CylinderShapeZ(r2, r2, hh)); // we can't add Triangle Mesh Shape to Compound Shape. Add one or two boxes float hbl = new Vector2(l, r1 - r2).magnitude * 0.5f; Vector3 n = new Vector3(l, r1 - r2, 0); n.Normalize(); Vector3 beg = new Vector3(0, 0, hh) + n * (r1 - r2); Vector3 beg2 = new Vector3(-beg.X, beg.Y, beg.Z); Vector3 end = new Vector3(0, -l, hh); float angle = math.atan2(n.Y, n.X); bool onlyFront = true; bool rev = (flipper.data.StartAngle < 0 | flipper.data.StartAngle > 180); if (!onlyFront || rev) { cs.AddChildShape( Matrix.RotationZ(-angle) * Matrix.Translation((beg + end) * 0.5f), new BoxShape(Mathf.Min(r1, r2), hbl, hh)); } if (!onlyFront || !rev) { cs.AddChildShape( Matrix.RotationZ(angle) * Matrix.Translation((beg2 + end) * 0.5f), new BoxShape(Mathf.Min(r1, r2), hbl, hh)); } return(cs); }
public void RenderSoftBody(SoftBody softBody) { Cull cullMode = device.GetRenderState <Cull>(RenderState.CullMode); device.SetRenderState(RenderState.CullMode, Cull.None); AlignedFaceArray faces = softBody.Faces; int faceCount = faces.Count; if (faceCount > 0) { PositionedNormal[] vectors = new PositionedNormal[faceCount * 3]; int v = 0; for (int i = 0; i < faceCount; i++) { NodePtrArray nodes = faces[i].Nodes; Node n0 = nodes[0]; Node n1 = nodes[1]; Node n2 = nodes[2]; vectors[v].Position = n0.Position; vectors[v].Normal = n0.Normal; vectors[v + 1].Position = n1.Position; vectors[v + 1].Normal = n1.Normal; vectors[v + 2].Position = n2.Position; vectors[v + 2].Normal = n2.Normal; v += 3; } device.VertexFormat = VertexFormat.PositionNormal; device.DrawUserPrimitives(PrimitiveType.TriangleList, faces.Count, vectors); } else { AlignedTetraArray tetras = softBody.Tetras; int tetraCount = tetras.Count; if (tetraCount > 0) { PositionedNormal[] vectors = new PositionedNormal[tetraCount * 12]; int v = 0; for (int i = 0; i < tetraCount; i++) { NodePtrArray nodes = tetras[i].Nodes; BulletSharp.Math.Vector3 v0 = nodes[0].Position; BulletSharp.Math.Vector3 v1 = nodes[1].Position; BulletSharp.Math.Vector3 v2 = nodes[2].Position; BulletSharp.Math.Vector3 v3 = nodes[3].Position; BulletSharp.Math.Vector3 v10 = v1 - v0; BulletSharp.Math.Vector3 v02 = v0 - v2; BulletSharp.Math.Vector3 normal = BulletSharp.Math.Vector3.Cross(v10, v02); normal.Normalize(); vectors[v].Position = v0; vectors[v].Normal = normal; vectors[v + 1].Position = v1; vectors[v + 1].Normal = normal; vectors[v + 2].Position = v2; vectors[v + 2].Normal = normal; normal = BulletSharp.Math.Vector3.Cross(v10, v3 - v0); normal.Normalize(); vectors[v + 3].Position = v0; vectors[v + 3].Normal = normal; vectors[v + 4].Position = v1; vectors[v + 4].Normal = normal; vectors[v + 5].Position = v3; vectors[v + 5].Normal = normal; normal = BulletSharp.Math.Vector3.Cross(v2 - v1, v3 - v1); normal.Normalize(); vectors[v + 6].Position = v1; vectors[v + 6].Normal = normal; vectors[v + 7].Position = v2; vectors[v + 7].Normal = normal; vectors[v + 8].Position = v3; vectors[v + 8].Normal = normal; normal = BulletSharp.Math.Vector3.Cross(v02, v3 - v2); normal.Normalize(); vectors[v + 9].Position = v2; vectors[v + 9].Normal = normal; vectors[v + 10].Position = v0; vectors[v + 10].Normal = normal; vectors[v + 11].Position = v3; vectors[v + 11].Normal = normal; v += 12; } device.VertexFormat = VertexFormat.PositionNormal; device.DrawUserPrimitives(PrimitiveType.TriangleList, tetraCount * 4, vectors); } else if (softBody.Links.Count > 0) { AlignedLinkArray links = softBody.Links; int linkCount = links.Count; int linkColor = System.Drawing.Color.Black.ToArgb(); device.VertexFormat = VertexFormat.Position | VertexFormat.Diffuse; PositionColored[] linkArray = new PositionColored[linkCount * 2]; for (int i = 0; i < linkCount; i++) { Link link = links[i]; linkArray[i * 2].Position = link.Nodes[0].Position; linkArray[i * 2].Color = linkColor; linkArray[i * 2 + 1].Position = link.Nodes[1].Position; linkArray[i * 2 + 1].Color = linkColor; } device.DrawUserPrimitives(PrimitiveType.LineList, links.Count, linkArray); } } device.SetRenderState(RenderState.CullMode, cullMode); }