-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChimp.cs
174 lines (138 loc) · 5.16 KB
/
Chimp.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
using System;
using System.Runtime.InteropServices;
// chipmunk types
using cpFloat = System.Double;
using cpBool = System.Boolean;
// we use these for documenting
using cpPtrIn = System.IntPtr; //default input ptr alias, don't use directly (grayed out)
using cpSpaceP = System.IntPtr;
using cpShapeP = System.IntPtr;
using cpBodyP = System.IntPtr;
using cpArbiterP = System.IntPtr;
using cpConstraintP = System.IntPtr;
//TODO namespace
//[System.Security.SuppressUnmanagedCodeSecurity] //TODO uncomment for performance
public class ChimpPtr
{
public readonly IntPtr pointer;
public ChimpPtr(IntPtr ptr)
{
pointer = ptr;
}
public static implicit operator IntPtr(ChimpPtr obj)
{
return obj.pointer;
}
public static implicit operator ChimpPtr(IntPtr ptr)
{
return new ChimpPtr(ptr);
}
internal const string F = "chipmunk.dll";
//Space
[DllImport(F)] protected static extern cpSpaceP cpSpaceNew();
[DllImport(F)] protected static extern void cpSpaceStep(cpSpaceP space, cpFloat dt);
[DllImport(F)] protected static extern void cpSpaceSetGravity(cpSpaceP space, cpVect gravity);
[DllImport(F)] protected static extern cpBodyP cpSpaceGetStaticBody(cpSpaceP space);
[DllImport(F)] protected static extern cpShapeP cpSpaceAddShape(cpSpaceP space, cpShapeP shape);
[DllImport(F)] protected static extern cpBodyP cpSpaceAddBody(cpSpaceP space, cpBodyP body);
//Shape
[DllImport(F)] protected static extern void cpShapeSetFriction(cpShapeP shape, cpFloat friction);
[DllImport(F)] protected static extern cpShapeP cpSegmentShapeNew(cpBodyP body, cpVect a, cpVect b, cpFloat radius);
[DllImport(F)] protected static extern cpShapeP cpCircleShapeNew(cpBodyP body, cpFloat radius, cpVect offset);
//Body
[DllImport(F)] protected static extern cpBodyP cpBodyNew(cpFloat mass, cpFloat moment);
[DllImport(F)] protected static extern void cpBodySetPosition(cpBodyP body, cpVect pos);
[DllImport(F)] protected static extern cpVect cpBodyGetPosition(cpBodyP body);
[DllImport(F)] protected static extern cpVect cpBodyGetVelocity(cpBodyP body);
}
public static class cpUtil
{
private const string F = ChimpPtr.F;
//Misc
public static Func<cpFloat, cpFloat, cpFloat, cpVect, cpFloat> MomentForCircle = cpMomentForCircle; //TODO gen these auto
[DllImport(F)] public static extern cpFloat cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset);
[DllImport(F)] public static extern cpFloat cpAreaForCircle(cpFloat r1, cpFloat r2);
[DllImport(F)] public static extern cpFloat cpMomentForSegment(cpFloat m, cpVect a, cpVect b, cpFloat radius);
[DllImport(F)] public static extern cpFloat cpAreaForSegment(cpVect a, cpVect b, cpFloat radius);
[DllImport(F)] public static extern cpFloat cpMomentForPoly(cpFloat m, int count, cpVect[] verts, cpVect offset, cpFloat radius);
[DllImport(F)] public static extern cpFloat cpAreaForPoly(int count, cpVect[] verts, cpFloat radius);
[DllImport(F)] public static extern cpVect cpCentroidForPoly(int count, cpVect[] verts);
[DllImport(F)] public static extern cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height);
}
public class cpSpace : ChimpPtr
{
public cpSpace() : base(cpSpaceNew())
{
}
public cpBody StaticBody => new cpBody(cpSpaceGetStaticBody(pointer)); //TODO cache
public void SetGravity(cpVect gv)
{
cpSpaceSetGravity(pointer, gv);
}
public void AddShape(cpShape shape)
{
cpSpaceAddShape(pointer, shape);
}
public void AddBody(cpBody body)
{
cpSpaceAddBody(pointer, body);
}
public void Step(cpFloat dt)
{
cpSpaceStep(pointer, dt);
}
}
public class cpShape : ChimpPtr
{
public cpShape(cpShapeP shapePtr) : base (shapePtr)
{
}
public static cpShape NewSegment(cpBody body, cpVect a, cpVect b, cpFloat r)
{
return new cpShape(cpSegmentShapeNew(body, a, b, r));
}
public static cpShape NewCircle(cpBodyP body, cpFloat radius, cpVect offset)
{
return new cpShape(cpCircleShapeNew(body, radius, offset));
}
public void SetFriction(cpFloat f)
{
cpShapeSetFriction(pointer, f);
}
}
public class cpBody : ChimpPtr
{
public cpBody(cpFloat mass, cpFloat moment) : base(cpBodyNew(mass, moment))
{
}
public cpBody(cpBodyP bodyPtr) : base(bodyPtr)
{
}
public cpVect Position
{
get => cpBodyGetPosition(pointer);
set => cpBodySetPosition(pointer, value);
}
public cpVect Velocity
{
get => cpBodyGetVelocity(pointer);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct cpVect
{
public cpFloat X;
public cpFloat Y;
public static cpVect Zero = new cpVect(0,0);
public cpVect(cpFloat x, cpFloat y)
{
X = x;
Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct ChimpTransform
{
public cpVect Position;
public float Angle;
}