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); }
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); } }
// 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 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)); }
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; } }