public Polar2(Vector2 point) { Polar2 polar = CartesianToPolar(point); angle = polar.angle; radius = polar.radius; }
/// <summary> /// Splits the cell /// </summary> private void Split() { //Update Self food *= .5f; Polar2 forcePolar = new Polar2(forceToAdd, (duplicationAngle + transform.eulerAngles.z) * Mathf.Deg2Rad); rgb2D.AddForce(forcePolar.cartesian); //Create split model CellModel splitModel = new CellModel(); splitModel.generation = model.generation + 1; splitModel.food = food; splitModel.foodToGrow = foodToGrow; splitModel.foodToDie = foodToDie; splitModel.duplicationAngle = duplicationAngle; splitModel.color = color; //Update Split Controller float evolveNum = UnityEngine.Random.Range(0f, 1f); if (evolveNum < model.mutationChance) { int randomNum = UnityEngine.Random.Range(0, 2); model.color = new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), .8f); if (randomNum == 0) { var splitController = Controller.Instantiate <PhagocyteController>("phagocyte", splitModel); splitController.transform.position = transform.position; splitController.transform.eulerAngles = new Vector3(0, 0, duplicationAngle + transform.eulerAngles.z); splitController.GetComponent <Rigidbody2D>().AddForce(-forcePolar.cartesian); } else { var splitController = Controller.Instantiate <PhotocyteController>("photocyte", splitModel); splitController.transform.position = transform.position; splitController.transform.eulerAngles = new Vector3(0, 0, duplicationAngle + transform.eulerAngles.z); splitController.GetComponent <Rigidbody2D>().AddForce(-forcePolar.cartesian); } } else { var splitController = Controller.Instantiate <PhotocyteController>("photocyte", splitModel); splitController.transform.position = transform.position; splitController.transform.eulerAngles = new Vector3(0, 0, duplicationAngle + transform.eulerAngles.z); splitController.GetComponent <Rigidbody2D>().AddForce(-forcePolar.cartesian); } }
//--------------Static Functions----------------// /// <summary> /// the angle difference from angle1 to angle2 (in radians) /// </summary> /// <param name="angle1"></param> /// <param name="angle2"></param> /// <returns></returns> public static float Angle(float angle1, float angle2) { Polar2 angle1Pol = new Polar2(1, angle1); Polar2 angle2Pol = new Polar2(1, angle2); return(Vector2.Angle(angle1Pol.cartesian, angle2Pol.cartesian) * Mathf.Deg2Rad); }
/// <summary> /// Converts a <see cref="Vector3"/> in world space to a <see cref="Polar2"/>. /// </summary> /// <param name="v">World space vector.</param> /// <param name="sphere">The sphere on which the vector is on.</param> /// <returns></returns> public static Polar2 ToPolar2(this Vector3 v, Transform sphere) { Vector3 pos = v - sphere.position; Vector3 azimuthReference = Quaternion.Euler(sphere.rotation.eulerAngles.x, sphere.rotation.eulerAngles.y, sphere.rotation.eulerAngles.z) * new Vector3(0, 0, -1 * (sphere.localScale.z / 2)); //Vector3 rightAzimuthReference = Quaternion.Euler(sphere.rotation.eulerAngles.x, sphere.rotation.eulerAngles.y, sphere.rotation.eulerAngles.z) * new Vector3(sphere.localScale.x, 0, 0); Vector3 zenithReference = Quaternion.Euler(sphere.rotation.eulerAngles.x, sphere.rotation.eulerAngles.y, sphere.rotation.eulerAngles.z) * (Vector3.up * (sphere.localScale.x / 2)); Polar2 polar = new Polar2( Vector3.Angle(zenithReference, pos), Vector3.SignedAngle(azimuthReference, Vector3.ProjectOnPlane(v - sphere.position, zenithReference), zenithReference)); return(polar); }
protected virtual void Emit(ParticleEmitter emitter, Particle particle) { Matrix transform = Matrix.CreateRotationZ(emitter.WorldRotation); // Generating spawn position. float emitAngle = RandomHelper.Next(0, MathHelper.TwoPi); float emitRadius = EmitRadius.GetValue(); Vector2 p = EmitPosition.GetValue() + new Polar2(emitRadius, emitAngle).ToVector2(); // Generating initial velocity. float initialAngle = EmitAngle.GetValue(); float initialVelocity = InitialVelocity.GetValue(); Vector2 v; if (InitialVelocityMode == InitialVelocityMode.AwayFromCenter && p != Vector2.Zero) { v = Vector2.Normalize(p) * initialVelocity; } else { v = new Polar2(initialVelocity, initialAngle).ToVector2(); } particle.Position = emitter.WorldPosition + Vector2.Transform(p, transform); particle.Rotation = emitter.WorldRotation + InitialRotation.GetValue(); particle.Velocity = Vector2.Transform(v, transform); particle.AngularVelocity = InitialAngularVelocity.GetValue(); particle.Duration = Duration.GetValue(); particle.Timer = 0; particle.Scale = InitialScale.GetValue(); if (SpriteFrames != null) { particle.Data = RandomHelper.Next(0, SpriteFrames.Length); } if (ColorMode == ParticleColorMode.Random) { particle.Color = Colors[RandomHelper.Next(0, Colors.Length)].ToVector4(); } else { particle.Color = Colors[0].ToVector4(); } }
/// <summary> /// Converts Cartesian coords to polar coords with angle in radians /// </summary> /// <param name="point">Cartesian coordinate</param> /// <returns></returns> public static Polar2 CartesianToPolar(Vector2 point) { Polar2 polar; float angle = Mathf.Atan(point.y / point.x); if (point.x < 0) { angle += Mathf.PI; } else if (point.y < 0) { angle += Mathf.PI * 2; } polar = new Polar2(point.magnitude, angle); return(polar); }
//public int pixHeight; //public float xOrg; //public float yOrg; //public float scale = 1.0F; /// <summary> /// Return the displacement of from 0 /// </summary> /// <param name="seed"></param> /// <param name="polar">polar coords with radius and angle in radians</param> /// <returns></returns> public static float GetTerrian(string seed, Polar2 polar) { float fDetial = 1; float fWeight = 10000f; float fValue = 0; int nOctaves = 10; float scale = .00005f; int disp = seed.Length; polar.radius *= scale; Vector2 coord = polar.cartesian; for (int i = 0; i < nOctaves; i++) { fValue += (Mathf.PerlinNoise(((float)coord.x + disp) * fDetial, ((float)coord.y + disp) * fDetial) - .5f) * fWeight; //Sum wegihted noise value fWeight *= .3f; //adjust weight fDetial *= 3; } return(fValue); }
public void DrawLine(Vector2 start, Vector2 end, PrimitiveBrush brush) { int startIndex = _vertexCount; int lod = 16; _borderColor = brush.BorderColor; _fillColor = Color.Transparent; Vector2 t = end - start; t = Vector2.Normalize(new Vector2(-t.Y, t.X)) * brush.BorderThickness * 0.5f; Polar2 a = Polar2.CreateFromVector(t); _edge = brush.BorderThickness * 0.5f * _edgeSoftness; int s0 = _vertexCount; PushVertex(start); PushVertex(end); _edge = 0; int s1 = _vertexCount; PushVerticesForCircle(start, new Vector2(a.R), a.Theta, a.Theta + MathHelper.Pi, lod); int s2 = _vertexCount; PushVerticesForCircle(end, new Vector2(a.R), a.Theta + MathHelper.Pi, a.Theta + MathHelper.TwoPi, lod); int s3 = _vertexCount; PushIndicesForCircle(s0 + 0, s1, lod, false); PushIndicesForCircle(s0 + 1, s2, lod, false); PushIndicesForRectangle(s3 - 1, s1, s0 + 1, s0); PushIndicesForRectangle(s0 + 1, s0, s2, s2 - 1); }
/// <summary> /// Creates x and y variable from polar coords /// </summary> /// <param name="polar">Polar coords</param> /// <returns></returns> public static Vector2 PolarToCartesian(Polar2 polar) { return(new Vector2(polar.radius * Mathf.Cos(polar.angle), polar.radius * Mathf.Sin(polar.angle))); }
public static float SqrDistance(Polar2 a, Polar2 b) { return(a.r * a.r + b.r * b.r - (2 * a.r * b.r * Mathf.Cos(a.d - b.d))); }
private void OnDrawGizmos() { Gizmos.matrix = transform.localToWorldMatrix; Gizmos.color = Color.red; var start = new Polar2(outerRadius, 0); var last = (Vector3)start; var current = start; var turnRad = turns * Mathf.PI * 2; if (stepSize < MIN_STEP) { stepSize = MIN_STEP; } var numSteps = turnRad / stepSize; if (numSteps >= 1) { for (var i = 0; i < numSteps; i++) { var p = i / numSteps; current.r = Mathf.Lerp(outerRadius, 0, curve.Evaluate(p)); current.d += stepSize; var c = (Vector3)current; Gizmos.DrawLine(last, c); last = c; } } //for( var i = 0; i < 200; i++ ) { // var p = i / 200f; // current.r = Mathf.Lerp( start.r, 0, Mathf.Log( p ) ); // current.d += stepSize; // Gizmos.DrawLine( last, current ); // last = current; //} //var pitchRad = pitch * Mathf.Deg2Rad; //for( var i = 0; i < 200; i++ ) { // var r = a * Mathf.Pow( E, pitchRad * ( 1 / Mathf.Tan( b ) ) ); // var p = new Polar2(); //} // float minTheta = Mathf.Log( 0.1f / a ) / b; // float delta = 5 * Mathf.Deg2Rad; // Polar2 last = Polar2.zero; // if( a <= 0 ) a = 0.1f; // if( b <= 0 ) b = 0.1f; // for( var theta = minTheta; ; theta += delta ) { // float r = a * Mathf.Exp( b * theta ); // var p = new Polar2( r, theta + startAngle ); // Gizmos.DrawLine( last, p ); // last = p; // if( r > outerRadius ) break; //} //for( var i = 0; i < 200; i++ ) { // var p = new Polar2( , ); // current.r = Mathf.Lerp( start.r, 0, Mathf.Log( p ) ); // current.d += stepSize; // Gizmos.DrawLine( last, current ); // last = current; //} }