AxiosEngine-old 

AxiosEngine-old Mercurial Source Tree


Root/axios/Factories/FixtureFactory.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
 
namespace FarseerPhysics.Factories
{
    /// <summary>
    /// An easy to use factory for creating bodies
    /// </summary>
    public static class FixtureFactory
    {
        public static Fixture AttachEdge(Vector2 start, Vector2 end, Body body)
        {
            return AttachEdge(start, end, body, null);
        }
 
        public static Fixture AttachEdge(Vector2 start, Vector2 end, Body body, object userData)
        {
            EdgeShape edgeShape = new EdgeShape(start, end);
            return body.CreateFixture(edgeShape, userData);
        }
 
        public static Fixture AttachLoopShape(Vertices vertices, Body body)
        {
            return AttachLoopShape(vertices, body, null);
        }
 
        public static Fixture AttachLoopShape(Vertices vertices, Body body, object userData)
        {
            LoopShape shape = new LoopShape(vertices);
            return body.CreateFixture(shape, userData);
        }
 
        public static Fixture AttachRectangle(float width, float height, float density, Vector2 offset, Body body,
                                              object userData)
        {
            Vertices rectangleVertices = PolygonTools.CreateRectangle(width / 2, height / 2);
            rectangleVertices.Translate(ref offset);
            PolygonShape rectangleShape = new PolygonShape(rectangleVertices, density);
            return body.CreateFixture(rectangleShape, userData);
        }
 
        public static Fixture AttachRectangle(float width, float height, float density, Vector2 offset, Body body)
        {
            return AttachRectangle(width, height, density, offset, body, null);
        }
 
        public static Fixture AttachCircle(float radius, float density, Body body)
        {
            return AttachCircle(radius, density, body, null);
        }
 
        public static Fixture AttachCircle(float radius, float density, Body body, object userData)
        {
            if (radius <= 0)
                throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters");
 
            CircleShape circleShape = new CircleShape(radius, density);
            return body.CreateFixture(circleShape, userData);
        }
 
        public static Fixture AttachCircle(float radius, float density, Body body, Vector2 offset)
        {
            return AttachCircle(radius, density, body, offset, null);
        }
 
        public static Fixture AttachCircle(float radius, float density, Body body, Vector2 offset, object userData)
        {
            if (radius <= 0)
                throw new ArgumentOutOfRangeException("radius", "Radius must be more than 0 meters");
 
            CircleShape circleShape = new CircleShape(radius, density);
            circleShape.Position = offset;
            return body.CreateFixture(circleShape, userData);
        }
 
        public static Fixture AttachPolygon(Vertices vertices, float density, Body body)
        {
            return AttachPolygon(vertices, density, body, null);
        }
 
        public static Fixture AttachPolygon(Vertices vertices, float density, Body body, object userData)
        {
            if (vertices.Count <= 1)
                throw new ArgumentOutOfRangeException("vertices", "Too few points to be a polygon");
 
            PolygonShape polygon = new PolygonShape(vertices, density);
            return body.CreateFixture(polygon, userData);
        }
 
        public static Fixture AttachEllipse(float xRadius, float yRadius, int edges, float density, Body body)
        {
            return AttachEllipse(xRadius, yRadius, edges, density, body, null);
        }
 
        public static Fixture AttachEllipse(float xRadius, float yRadius, int edges, float density, Body body,
                                            object userData)
        {
            if (xRadius <= 0)
                throw new ArgumentOutOfRangeException("xRadius", "X-radius must be more than 0");
 
            if (yRadius <= 0)
                throw new ArgumentOutOfRangeException("yRadius", "Y-radius must be more than 0");
 
            Vertices ellipseVertices = PolygonTools.CreateEllipse(xRadius, yRadius, edges);
            PolygonShape polygonShape = new PolygonShape(ellipseVertices, density);
            return body.CreateFixture(polygonShape, userData);
        }
 
        public static List<Fixture> AttachCompoundPolygon(List<Vertices> list, float density, Body body)
        {
            return AttachCompoundPolygon(list, density, body, null);
        }
 
        public static List<Fixture> AttachCompoundPolygon(List<Vertices> list, float density, Body body, object userData)
        {
            List<Fixture> res = new List<Fixture>(list.Count);
 
            //Then we create several fixtures using the body
            foreach (Vertices vertices in list)
            {
                if (vertices.Count == 2)
                {
                    EdgeShape shape = new EdgeShape(vertices[0], vertices[1]);
                    res.Add(body.CreateFixture(shape, userData));
                }
                else
                {
                    PolygonShape shape = new PolygonShape(vertices, density);
                    res.Add(body.CreateFixture(shape, userData));
                }
            }
 
            return res;
        }
 
        public static List<Fixture> AttachLineArc(float radians, int sides, float radius, Vector2 position, float angle,
                                                  bool closed, Body body)
        {
            Vertices arc = PolygonTools.CreateArc(radians, sides, radius);
            arc.Rotate((MathHelper.Pi - radians) / 2 + angle);
            arc.Translate(ref position);
 
            List<Fixture> fixtures = new List<Fixture>(arc.Count);
 
            if (closed)
            {
                fixtures.Add(AttachLoopShape(arc, body));
            }
 
            for (int i = 1; i < arc.Count; i++)
            {
                fixtures.Add(AttachEdge(arc[i], arc[i - 1], body));
            }
 
            return fixtures;
        }
 
        public static List<Fixture> AttachSolidArc(float density, float radians, int sides, float radius,
                                                   Vector2 position, float angle, Body body)
        {
            Vertices arc = PolygonTools.CreateArc(radians, sides, radius);
            arc.Rotate((MathHelper.Pi - radians) / 2 + angle);
 
            arc.Translate(ref position);
 
            //Close the arc
            arc.Add(arc[0]);
 
            List<Vertices> triangles = EarclipDecomposer.ConvexPartition(arc);
 
            return AttachCompoundPolygon(triangles, density, body);
        }
    }
}
Source at commit 5b5b96a46617 created 12 years 8 months ago.
By nathan@daedalus, Adding a check to only tick a timer if the window is active

Archive Download this file

Page rendered in 0.86629s using 11 queries.