private FVector2 GetConstantSpeed(FVector2 startPoint, FVector2 endPoint, float travelTime) { FVector2 distanceVector = endPoint - startPoint; FVector2 speedVector = new FVector2(distanceVector.X / travelTime, distanceVector.Y / travelTime); return(speedVector); }
public void AddRelativeMovementEvent(FVector2 distanceVector, float traveltime, int interType = 0, float delay = 0.0f, bool guaranteeArrival = false) { SetupPhysics(); FVector2 destination = new FVector2(body.Position.X + distanceVector.X, body.Position.Y + distanceVector.Y); AddMovementEvent(destination, traveltime, interType, delay, guaranteeArrival); }
protected virtual bool OnCollisionEvent(Fixture fixtureA, Fixture fixtureB, Contact contact) { if (fixtureB.Body.UserFSBodyComponent.gameObject.tag == "Player") { //print ("IMPACT!!"); FVector2 playerPos = fixtureB.Body.Position; FVector2 explosionPos = body.Position; FVector2 result = playerPos - explosionPos; float distance = result.Length(); result.Normalize(); float playerImpact = result.Y * distance * (maxImpactOnPlayer - minImpactOnPlayer) / maxRadius; fixtureB.Body.LinearVelocity = new FVector2(fixtureB.Body.LinearVelocity.X, minImpactOnPlayer + playerImpact); } else { Health objectHealth = fixtureB.Body.UserFSBodyComponent.gameObject.GetComponent <Health>(); if (objectHealth != null) { objectHealth.Damage(GetDamage()); } } return(false); }
public static Scheduler SpawnEnemy(GameObject objectToSpawn, FVector2 position) { GameObject newObj = (GameObject)Instantiate(objectToSpawn, new Vector3(position.X, position.Y, 0.0f), Quaternion.identity); newObj.AddComponent("Scheduler"); return(newObj.GetComponent <Scheduler>()); }
public static PolygonShape CreateEqlTriangle(float sideLength, float density) { FVector2[] pointList = new FVector2[3]; pointList[0] = new FVector2(0.0f, sideLength * 0.25f * SR3); pointList[1] = new FVector2(sideLength * -0.5f, sideLength * -0.25f * SR3); pointList[2] = new FVector2(sideLength * 0.5f, sideLength * -0.25f * SR3); PolygonShape shape = new PolygonShape(new Vertices(pointList), density); return(shape); }
public void SetSpeed(float newSpeed) { projectileSpeed = newSpeed; if (normalizedVelocity == null || normalizedVelocity.Length() == 0.0f) { //if no direction, assume straight upwards normalizedVelocity = new FVector2(0.0f, 1.0f); } body.LinearVelocity = new FVector2(projectileSpeed * normalizedVelocity.X, projectileSpeed * normalizedVelocity.Y); }
private void UpdateShotAngle() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 temp3 = ray.origin - gameObject.transform.position; FVector2 temp2 = new FVector2(temp3.x, temp3.y); temp2.Normalize(); foreach (Weapon weap in weaponList) { weap.SetDirection(temp2); } }
//MUST BE NORMALIZED VECTOR public void SetDirection(FVector2 newDir) { SetupPhysics(); normalizedVelocity = new FVector2(newDir.X, newDir.Y); body.LinearVelocity = new FVector2(projectileSpeed * newDir.X, projectileSpeed * newDir.Y); float angleInRads = Mathf.Acos(newDir.X); if (newDir.Y < 0.0f) { angleInRads = 2 * Mathf.PI - angleInRads; } body.Rotation = angleInRads + (0.5f) * Mathf.PI; }
public static PolygonShape CreateDiamond(float height, float width, float density) { if (height <= 0.0f || width <= 0.0f) { print("INVALID SIZE VALUE FOR DIAMOND CLASS"); } FVector2[] pointList = new FVector2[4]; pointList[0] = new FVector2(0.0f, height / 2.0f); pointList[1] = new FVector2(-width / 2.0f, 0.0f); pointList[2] = new FVector2(0.0f, -height / 2.0f); pointList[3] = new FVector2(width / 2.0f, 0.0f); PolygonShape shape = new PolygonShape(new Vertices(pointList), density); return(shape); }
private void SetupPhysics() { if (body == null) { body = GetComponent <FSBodyComponent>().PhysicsBody; body.OnCollision += OnCollisionEvent; Vertices vertices = TriangleIsosceles.CreateVertexList(length, angle); FVector2 firstPoint = vertices.NextVertex(0); vertices.Translate(new FVector2(-firstPoint.X, -firstPoint.Y)); vertices.Rotate(Mathf.PI / 180.0f * angle); PolygonShape polyTemp = new PolygonShape(vertices, 1.0f); body.CreateFixture(polyTemp); } }
// Update is called once per frame void FixedUpdate() { lifespan -= Time.fixedDeltaTime; if (lifespan <= 0.0f) { GameObject.Destroy(gameObject); } if (body.LinearVelocity.LengthSquared() < minSquaredVelocity) { FVector2 temp = body.LinearVelocity; temp.Normalize(); body.LinearVelocity = new FVector2(temp.X * minVelocity, temp.Y * minVelocity); } }
public static PolygonShape CreateFanPiece(float sideLength, float interiorAngle, float rotationAngle) { FVector2[] pointList = new FVector2[3]; interiorAngle = Mathf.PI / 180.0f * interiorAngle; rotationAngle = Mathf.PI / 180.0f * rotationAngle; pointList[0] = new FVector2(0.0f, 0.0f); pointList[1] = new FVector2(sideLength, 0.0f); pointList[2] = new FVector2(sideLength * Mathf.Cos(interiorAngle), sideLength * Mathf.Sin(interiorAngle)); Vertices vertices = new Vertices(pointList); vertices.Rotate(rotationAngle); return(new PolygonShape(vertices, 1.0f)); }
public void AddMovementEvent(FVector2 destination, float travelTime, int interpType = 0, float delay = 0.0f, bool guaranteeArrival = false) { SetupPhysics(); MovementEvent move = new MovementEvent(); move.destination = destination; move.travelTime = travelTime; move.interpType = interpType; move.delay = delay; move.guaranteeArrival = guaranteeArrival; movementEventList.AddLast(move); if (!movementActive) { StartMovementEvent(); } }
public void SetDirection(FVector2 newDir) { newDir.Normalize(); direction = new FVector2(newDir.X, newDir.Y); float zAngle = Mathf.Acos(newDir.X) * 180.0f / Mathf.PI; if (newDir.Y < 0.0f) { zAngle = 360.0f - zAngle; } zAngle -= 90.0f; //Up is our 0 degrees here :P gameObject.transform.rotation = Quaternion.identity; gameObject.transform.Rotate(new Vector3(0.0f, 0.0f, zAngle)); }
public override void InitJoint() { base.InitJoint(); //Microsoft.Xna.Framework.FVector2 angleV = new Microsoft.Xna.Framework.FVector2(BodyB.PhysicsBody.Position.X - BodyA.PhysicsBody.Position.X, BodyB.PhysicsBody.Position.Y - BodyA.PhysicsBody.Position.Y); float ang = Mathf.Atan2(BodyB.PhysicsBody.Position.Y - BodyA.PhysicsBody.Position.Y, BodyB.PhysicsBody.Position.X - BodyA.PhysicsBody.Position.X); Microsoft.Xna.Framework.FVector2 angleV = new Microsoft.Xna.Framework.FVector2(Mathf.Cos(ang), Mathf.Sin(ang)); //angleV.Normalize(); joint = FarseerPhysics.Factories.JointFactory.CreatePrismaticJoint(FSWorldComponent.PhysicsWorld, BodyA.PhysicsBody, BodyB.PhysicsBody, Microsoft.Xna.Framework.FVector2.Zero, angleV); joint.CollideConnected = CollideConnected; //joint.Frequency = Frequency; //joint.DampingRatio = 0.5f; d }
//More efficient b/c uses Sin/Cos only once public static PolygonShape[] CreateFanSet(float radius, float fanAngle, int shardCount, int density) { PolygonShape[] shapeList = new PolygonShape[shardCount]; float interiorAngle = fanAngle / ((float)shardCount) * Mathf.PI / 180.0f; FVector2[] pointList = new FVector2[3]; pointList[0] = new FVector2(0.0f, 0.0f); pointList[1] = new FVector2(radius, 0.0f); pointList[2] = new FVector2(radius * Mathf.Cos(interiorAngle), radius * Mathf.Sin(interiorAngle)); Vertices vertices = new Vertices(pointList); for (int index = 0; index < shardCount; index++) { shapeList[index] = new PolygonShape(vertices, density); //PolygonShape makes a deep copy of vertices so I can reuse this sucker vertices.Rotate(interiorAngle); } return(shapeList); }
private IEnumerator MovementEventHelper(MovementEvent move) { //print ("currentPosition: " + body.Position); //print ("should be moving"); if (move.delay > 0.0f) { yield return(new WaitForSeconds(move.delay)); } FVector2 speed = GetConstantSpeed(body.Position, move.destination, move.travelTime); body.LinearVelocity = speed; //Wait for movement to finish yield return(new WaitForSeconds(move.travelTime)); //We're done, stop the object body.LinearVelocity = FVector2.Zero; if (move.guaranteeArrival) { body.Position = move.destination; } }
private void SetupPhysics() { if (body == null) { body = GetComponent <FSBodyComponent>().PhysicsBody; body.FixedRotation = true; //body.IsSensor = !isBouncy; body.IsBullet = true; if (!hasGravity) { body.GravityScale = 0.0f; } body.OnCollision += OnCollisionEvent; Vector3 objectRotation = gameObject.transform.eulerAngles; //body.Rotation = objectRotation.z + (0.5f)*Mathf.PI; float a = objectRotation.z * Mathf.PI / 180.0f + Mathf.PI / 2.0f; normalizedVelocity = new FVector2(Mathf.Cos(a), Mathf.Sin(a)); SetSpeed(projectileSpeed); } }
public static Vertices CreateVertexList(float sideLength, float angle) { if (sideLength <= 0.0f) { print("SIDE LENGTH FOR ISOSCELES TRIANGLE CLASS MUST BE POSITIVE"); } if (angle <= 0.0f || angle >= 180.0f) { print("INVALID ANGLE VALUE FOR ISOSCELES TRIANGLE CLASS"); } FVector2[] pointList = new FVector2[3]; float theta = Mathf.PI / 360.0f * angle; //using half the angle size float halfHeight = sideLength * Mathf.Cos(theta) / 2.0f; float halfWidth = sideLength * Mathf.Sin(theta); pointList[0] = new FVector2(0.0f, halfHeight); pointList[1] = new FVector2(-halfWidth, -halfHeight); pointList[2] = new FVector2(halfWidth, -halfHeight); return(new Vertices(pointList)); }
public FVector3(FVector2 value, float z) { X = value.X; Y = value.Y; Z = z; }
void Update() { Init(); if (lastContacts.Count < 1) { guiText.text = "Contact: null"; } else { string guiOutput = ""; float weight = 0f; foreach (Contact lastContact in lastContacts) { // get stats! bool isTouching = lastContact.IsTouching(); FarseerPhysics.Common.FixedArray2 <FVector2> contactPoints; FVector2 normal; string cps = "none"; string cn = "none"; string lpm = "none"; float dot = 0f; //float dot2 = 0f; if (isTouching) { lastContact.GetWorldManifold(out normal, out contactPoints); cps = string.Format("p0[ {0}, {1} ] p1[ {2}, {3} ]", contactPoints[0].X, contactPoints[0].Y, contactPoints[1].X, contactPoints[1].Y); cn = string.Format("[ {0}, {1} ]", normal.X, normal.Y); FarseerPhysics.Common.FixedArray2 <FarseerPhysics.Collision.ManifoldPoint> lpp = lastContact.Manifold.Points; lpm = string.Format("nimpulse[ {0}, {1} ] tanimpulse[ {2}, {3} ]", lpp[0].NormalImpulse / Time.fixedDeltaTime, lpp[1].NormalImpulse / Time.fixedDeltaTime, lpp[0].TangentImpulse / Time.fixedDeltaTime, lpp[1].TangentImpulse / Time.fixedDeltaTime); dot = FVector2.Dot(FVector2.Normalize(-AttachedBody.PhysicsBody.Position + contactPoints[0]), normal); //dot2 = FVector2.Dot(FVector2.Normalize(-AttachedBody.PhysicsBody.Position+contactPoints[1]), normal); weight += (1f * (lpp[0].NormalImpulse / Time.fixedDeltaTime) / 9.8f); weight += (1f * (lpp[1].NormalImpulse / Time.fixedDeltaTime) / 9.8f); } guiOutput += string.Format(contactInfoBase, lastContact.Restitution, lastContact.TangentSpeed, cps, cn, lpm, dot); } for (int i = 0; i < lastContacts.Count; i++) { if (!lastContacts[i].IsTouching()) { lastContacts.RemoveAt(i); i = Mathf.Max(0, i - 1); } } float ownmass = AttachedBody.PhysicsBody.Mass; weight -= ownmass; weight *= 0.5f; weight += ownmass; guiText.text = "TOTAL WEIGHT: " + weight.ToString() + "Kg"; guiText.text += guiOutput; } }