123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
-
- using System;
- using System.Diagnostics;
- using Microsoft.Xna.Framework;
- namespace Box2D.XNA
- {
- public class CircleShape : Shape
- {
- public CircleShape()
- {
- ShapeType = ShapeType.Circle;
- _radius = 0.0f;
- _p = Vector2.Zero;
- }
-
- public override Shape Clone()
- {
- CircleShape shape = new CircleShape();
- shape.ShapeType = ShapeType;
- shape._radius = _radius;
- shape._p = _p;
- return shape;
- }
-
- public override int GetChildCount()
- {
- return 1;
- }
-
- public override bool TestPoint(ref Transform transform, Vector2 p)
- {
- Vector2 center = transform.Position + MathUtils.Multiply(ref transform.R, _p);
- Vector2 d = p - center;
- return Vector2.Dot(d, d) <= _radius * _radius;
- }
-
-
-
-
- public override bool RayCast(out RayCastOutput output, ref RayCastInput input, ref Transform transform, int childIndex)
- {
- output = new RayCastOutput();
- Vector2 position = transform.Position + MathUtils.Multiply(ref transform.R, _p);
- Vector2 s = input.p1 - position;
- float b = Vector2.Dot(s, s) - _radius * _radius;
-
- Vector2 r = input.p2 - input.p1;
- float c = Vector2.Dot(s, r);
- float rr = Vector2.Dot(r, r);
- float sigma = c * c - rr * b;
-
- if (sigma < 0.0f || rr < Settings.b2_epsilon)
- {
- return false;
- }
-
- float a = -(c + (float)Math.Sqrt((double)sigma));
-
- if (0.0f <= a && a <= input.maxFraction * rr)
- {
- a /= rr;
- output.fraction = a;
- Vector2 norm = (s + a * r);
- norm.Normalize();
- output.normal = norm;
- return true;
- }
- return false;
- }
-
- public override void ComputeAABB(out AABB aabb, ref Transform transform, int childIndex)
- {
- Vector2 p = transform.Position + MathUtils.Multiply(ref transform.R, _p);
- aabb.lowerBound = new Vector2(p.X - _radius, p.Y - _radius);
- aabb.upperBound = new Vector2(p.X + _radius, p.Y + _radius);
- }
-
- public override void ComputeMass(out MassData massData, float density)
- {
- massData.mass = density * Settings.b2_pi * _radius * _radius;
- massData.center = _p;
-
- massData.I = massData.mass * (0.5f * _radius * _radius + Vector2.Dot(_p, _p));
- }
-
- public int GetVertexCount() { return 1; }
-
- public Vector2 GetVertex(int index)
- {
- Debug.Assert(index == 0);
- return _p;
- }
-
- public Vector2 _p;
- }
- }
|