private void FixedUpdate() { if (fuel > 0) { Vector3 p0 = car.transform.TransformPoint(new Vector3(LocalAnchorB.x, LocalAnchorB.y, -5f)); backBody.PhysicsBody.ApplyTorque(-movement * speed * Time.fixedDeltaTime); if (Input.GetKeyDown(KeyCode.Space)) { car.PhysicsBody.ApplyForce(jumpPow * FSHelper.Vector2ToFVector2(jumpForce), FSHelper.Vector3ToFVector2(p0)); } } else { StartCoroutine("OpenMenu"); } fuel -= fuelConsumption * Mathf.Abs(movement) * Time.fixedDeltaTime; if (Input.GetKey(KeyCode.K)) { car.PhysicsBody.ApplyTorque(-rotationPow * speed * Time.fixedDeltaTime); } if (Input.GetKey(KeyCode.J)) { car.PhysicsBody.ApplyTorque(rotationPow * speed * Time.fixedDeltaTime); } if (fuel <= 0) { Debug.Log("You have failed"); } }
public override void InitJoint() { base.InitJoint(); //B tekerlek //A araba //jesus %99 em-in Vector3 p0 = BodyB.transform.TransformPoint(new Vector3(LocalAnchorB.x, LocalAnchorB.y, -5f)); joint = JointFactory.CreateWheelJoint(FSWorldComponent.PhysicsWorld, BodyA.PhysicsBody, BodyB.PhysicsBody, BodyB.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p0)), FSHelper.Vector2ToFVector2(axis)); //UnitY ile yanlış yapıyor olabilirim joint.MaxMotorTorque = MaxMotorTorque; joint.MotorEnabled = MotorEnabled; joint.MotorSpeed = MotorSpeed; joint.SpringDampingRatio = springDampingRatio; joint.SpringFrequencyHz = springFrequency; joint.CollideConnected = false; // }
protected virtual void ConvertToConvex(FSConcaveShapeComponent targetCSC) { FSShapeComponent[] childcomps = targetCSC.GetComponentsInChildren <FSShapeComponent>(); if (childcomps != null) { if (childcomps.Length > 0) { for (int i = 0; i < childcomps.Length; i++) { if (childcomps[i] == null) { continue; } if (childcomps[i].gameObject == null) { continue; } DestroyImmediate(childcomps[i].gameObject); } } } // convert vertices FarseerPhysics.Common.Vertices concaveVertices = new FarseerPhysics.Common.Vertices(); if (targetCSC.PointInput == FSShapePointInput.Transform) { for (int i = 0; i < targetCSC.TransformPoints.Length; i++) { concaveVertices.Add(FSHelper.Vector3ToFVector2(targetCSC.TransformPoints[i].localPosition)); } } List <FarseerPhysics.Common.Vertices> convexShapeVs = FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(concaveVertices); for (int i = 0; i < convexShapeVs.Count; i++) { GameObject newConvShape = new GameObject("convexShape" + i.ToString()); newConvShape.transform.parent = targetCSC.transform; newConvShape.transform.localPosition = Vector3.zero; newConvShape.transform.localRotation = Quaternion.Euler(Vector3.zero); newConvShape.transform.localScale = Vector3.one; FSShapeComponent shape0 = newConvShape.AddComponent <FSShapeComponent>(); shape0.CollidesWith = targetCSC.CollidesWith; shape0.CollisionFilter = targetCSC.CollisionFilter; shape0.BelongsTo = targetCSC.BelongsTo; shape0.CollisionGroup = targetCSC.CollisionGroup; shape0.Friction = targetCSC.Friction; shape0.Restitution = targetCSC.Restitution; shape0.Density = targetCSC.Density; shape0.UseUnityCollider = false; shape0.PolygonPoints = new Transform[convexShapeVs[i].Count]; for (int j = 0; j < convexShapeVs[i].Count; j++) { GameObject pnew = new GameObject("p" + j.ToString()); pnew.transform.parent = shape0.transform; pnew.transform.localPosition = FSHelper.FVector2ToVector3(convexShapeVs[i][j]); shape0.PolygonPoints[j] = pnew.transform; } } }
private bool OnCollisionEvent(FSFixture fixtureA, FSFixture fixtureB, Contact contact) { FSBody bodyB = fixtureB.Body; //if (bodyB.UserTag == "Respawn") { FVector2 normal; FarseerPhysics.Common.FixedArray2 <FVector2> contactPoints; contact.GetWorldManifold(out normal, out contactPoints); bodyB.SetTransform(FSHelper.Vector3ToFVector2(RespawnPosition.position), 0f); bodyB.ResetDynamics(); //} return(true); }
public override void InitJoint() { base.InitJoint(); Vector3 p0 = BodyA.transform.TransformPoint(new Vector3(LocalAnchorA.x, LocalAnchorA.y, -5f)); Vector3 p1 = BodyB.transform.TransformPoint(new Vector3(LocalAnchorB.x, LocalAnchorB.y, -5f)); joint = JointFactory.CreatePulleyJoint(FSWorldComponent.PhysicsWorld, BodyA.PhysicsBody, BodyB.PhysicsBody, FSHelper.Vector3ToFVector2(GroundAnchorA.position), FSHelper.Vector3ToFVector2(GroundAnchorB.position), BodyA.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p0)), BodyB.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p1)), ForceRatio); joint.CollideConnected = CollideConnected; }
private PolygonShape GetPolyShape() { // if this GameObject contains the FSBodyComponent there is no need to adjust the colliders bool hasFsBody = GetComponent <FSBodyComponent>() != null; var shapePoints = new List <FVector2>(); if (UseUnityCollider) { if (!GetColliderMeasurements(hasFsBody, ref shapePoints)) { return(null); } } else { if (UseTransforms) { if (PolygonTransforms.Length < 3) { return(null); } foreach (Transform t in PolygonTransforms) { shapePoints.Add(hasFsBody ? FSHelper.Vector3ToFVector2(t.localPosition) : FSHelper.Vector3ToFVector2(FSHelper.LocalTranslatedVec3( transform.parent.InverseTransformPoint(t.position), transform.parent))); } } else { if (PolygonCoordinates.Length < 3) { return(null); } foreach (Vector2 t in PolygonCoordinates) { shapePoints.Add(hasFsBody ? FSHelper.Vector3ToFVector2(t) : FSHelper.Vector3ToFVector2(t)); } } } return(new PolygonShape(new Vertices(shapePoints.ToArray()), Density)); }
public override void InitJoint() { base.InitJoint(); // Vector3 p0 = BodyB.transform.TransformPoint(new Vector3(LocalAnchorB.x, LocalAnchorB.y, -5f)); joint = JointFactory.CreateRevoluteJoint(FSWorldComponent.PhysicsWorld, BodyA.PhysicsBody, BodyB.PhysicsBody, BodyB.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p0))); joint.CollideConnected = CollideConnected; joint.LowerLimit = LowerLimit * Mathf.Deg2Rad; joint.UpperLimit = UpperLimit * Mathf.Deg2Rad; joint.LimitEnabled = LimitEnabled; joint.MaxMotorTorque = MaxMotorTorque; joint.MotorSpeed = MotorSpeed; joint.MotorEnabled = MotorEnabled; }
private bool GetColliderMeasurements(bool hasFsBody, ref List <FVector2> shapePoints) { // TODO: Add support for capsule colliders var box = GetComponent <BoxCollider>(); if (box == null) { return(false); } Vector3 scale = transform.lossyScale; // Added support for collider center offset (has to convert to local first) var center = transform.InverseTransformPoint(box.bounds.center); // Top left to Top Right points, counter-clockwise var v00l = new Vector3(-box.size.x * 0.5f, -box.size.y * 0.5f) + center; var v01l = new Vector3(box.size.x * 0.5f, -box.size.y * 0.5f) + center; var v02l = new Vector3(box.size.x * 0.5f, box.size.y * 0.5f) + center; var v03l = new Vector3(-box.size.x * 0.5f, box.size.y * 0.5f) + center; v00l.Scale(scale); v01l.Scale(scale); v02l.Scale(scale); v03l.Scale(scale); Vector3 v00 = hasFsBody ? v00l : FSHelper.LocalTranslatedVec3(v00l, transform); Vector3 v01 = hasFsBody ? v01l : FSHelper.LocalTranslatedVec3(v01l, transform); Vector3 v02 = hasFsBody ? v02l : FSHelper.LocalTranslatedVec3(v02l, transform); Vector3 v03 = hasFsBody ? v03l : FSHelper.LocalTranslatedVec3(v03l, transform); shapePoints.Add(FSHelper.Vector3ToFVector2(v00)); shapePoints.Add(FSHelper.Vector3ToFVector2(v01)); shapePoints.Add(FSHelper.Vector3ToFVector2(v02)); shapePoints.Add(FSHelper.Vector3ToFVector2(v03)); return(true); }
public void ConvertToConvex() { FSShapeComponent[] childFsShapes = GetComponentsInChildren <FSShapeComponent>(); foreach (FSShapeComponent shapeComponent in childFsShapes) { if (shapeComponent.gameObject == null) { continue; } DestroyImmediate(shapeComponent.gameObject); } // convert vertices var concaveVertices = new FarseerPhysics.Common.Vertices(); if (PointInput == FSShapePointInput.Transform) { for (int i = 0; i < PointsTransforms.Length; i++) { concaveVertices.Add(FSHelper.Vector3ToFVector2(PointsTransforms[i].localPosition)); } } if (PointInput == FSShapePointInput.Vector2List) { foreach (var coordinate in PointsCoordinates) { concaveVertices.Add(FSHelper.Vector2ToFVector2(transform.TransformPoint(coordinate))); } } List <FarseerPhysics.Common.Vertices> convexShapeVs = FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(concaveVertices); for (int i = 0; i < convexShapeVs.Count; i++) { var newConvShape = new GameObject("convexShape" + i.ToString()); newConvShape.transform.parent = transform; newConvShape.transform.localPosition = Vector3.zero; newConvShape.transform.localRotation = Quaternion.Euler(Vector3.zero); newConvShape.transform.localScale = Vector3.one; var shapeComponent = newConvShape.AddComponent <FSShapeComponent>(); shapeComponent.CollidesWith = CollidesWith; shapeComponent.CollisionFilter = CollisionFilter; shapeComponent.BelongsTo = BelongsTo; shapeComponent.CollisionGroup = CollisionGroup; shapeComponent.Friction = Friction; shapeComponent.Restitution = Restitution; shapeComponent.Density = Density; shapeComponent.UseUnityCollider = false; shapeComponent.UseTransforms = (PointInput == FSShapePointInput.Transform); if (PointInput == FSShapePointInput.Transform) { shapeComponent.PolygonTransforms = new Transform[convexShapeVs[i].Count]; for (int j = 0; j < convexShapeVs[i].Count; j++) { var pnew = new GameObject("p" + j.ToString(CultureInfo.InvariantCulture)); pnew.transform.parent = shapeComponent.transform; pnew.transform.localPosition = FSHelper.FVector2ToVector3(convexShapeVs[i][j]); shapeComponent.PolygonTransforms[j] = pnew.transform; } } else { shapeComponent.PolygonCoordinates = new Vector2[convexShapeVs[i].Count]; for (int j = 0; j < convexShapeVs[i].Count; j++) { shapeComponent.PolygonCoordinates[j] = newConvShape.transform.InverseTransformPoint(FSHelper.FVector2ToVector3(convexShapeVs[i][j])); } } } }
protected PolygonShape GetPolyShape() { // if this GameObject contains the FSBodyComponent // then there is no need to adjust the colliders! bool isBody = GetComponent <FSBodyComponent>() != null; List <FVector2> vs = new List <FVector2>(); if (UseUnityCollider) { BoxCollider bc = this.GetComponent <BoxCollider>(); if (bc == null) { return(null); } Vector3 scale = transform.lossyScale; //Debug.Log("SCALE: " + scale); Vector3 v00l = new Vector3(-bc.size.x * 0.5f, -bc.size.y * 0.5f); Vector3 v01l = new Vector3(bc.size.x * 0.5f, -bc.size.y * 0.5f); Vector3 v02l = new Vector3(bc.size.x * 0.5f, bc.size.y * 0.5f); Vector3 v03l = new Vector3(-bc.size.x * 0.5f, bc.size.y * 0.5f); v00l.Scale(scale); v01l.Scale(scale); v02l.Scale(scale); v03l.Scale(scale); Vector3 v00 = isBody ? v00l : FSHelper.LocalTranslatedVec3(v00l, this.transform); //v00.Scale(scale); Vector3 v01 = isBody ? v01l : FSHelper.LocalTranslatedVec3(v01l, this.transform); //v01.Scale(scale); Vector3 v02 = isBody ? v02l : FSHelper.LocalTranslatedVec3(v02l, this.transform); //v02.Scale(scale); Vector3 v03 = isBody ? v03l : FSHelper.LocalTranslatedVec3(v03l, this.transform); //v03.Scale(scale); vs.Add(FSHelper.Vector3ToFVector2(v00)); vs.Add(FSHelper.Vector3ToFVector2(v01)); vs.Add(FSHelper.Vector3ToFVector2(v02)); vs.Add(FSHelper.Vector3ToFVector2(v03)); } else { if (PolygonPoints.Length < 3) { return(null); } //vs = new Vertices(); for (int i = 0; i < PolygonPoints.Length; i++) { if (!isBody) { // updated! // now the points can be anywhere Vector3 localp = transform.parent.InverseTransformPoint(PolygonPoints[i].position); vs.Add(FSHelper.Vector3ToFVector2(FSHelper.LocalTranslatedVec3(localp, this.transform.parent))); // not doing this way anymore because this way points couldn't be shared betweeen shapes //vs.Add(FSHelper.Vector3ToFVector2(FSHelper.LocalTranslatedVec3(PolygonPoints[i].localPosition, this.transform))); } else { vs.Add(FSHelper.Vector3ToFVector2(PolygonPoints[i].localPosition)); } } } return(new PolygonShape(new Vertices(vs.ToArray()), Density)); }