Addons updated to new doc format
[io.git] / addons / ODE / source / IoODEJoint.c
blob3c76dd0a338f62ee6a44e4fc30810c1f35cb3e2f
1 //metadoc ODEJoint Jonathan Wright", 2006)
2 //metadoc ODEJoint license BSD revised
3 /*metadoc ODEJoint description
4 ODEJoint binding
5 */
7 #include "IoODEJoint_internal.h"
8 #include "IoODEBody.h"
9 #include "IoState.h"
10 #include "IoSeq.h"
11 #include "IoVector_ode.h"
12 #include "GLIncludes.h"
14 /* ----------------------------------------------------------- */
16 IoTag *IoODEJoint_newTag(void *state)
18 // TODO: Get rid of IoODEJoint as an IoObject. Can't be manipulated directly through Io.
19 IoTag *tag = IoTag_newWithName_("ODEJoint");
20 IoTag_state_(tag, state);
21 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoODEJoint_free);
22 IoTag_markFunc_(tag, (IoTagMarkFunc *)IoODEJoint_mark);
23 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoODEJoint_rawClone);
24 return tag;
27 IoODEJoint *IoODEJoint_proto(void *state)
29 IoObject *self = IoObject_new(state);
30 IoObject_tag_(self, IoODEJoint_newTag(state));
32 IoODEJoint_protoCommon(self);
34 IoState_registerProtoWithFunc_(state, self, IoODEJoint_proto);
37 IoMethodTable methodTable[] = {
38 ODE_COMMON_JOINT_METHODS
39 {NULL, NULL},
41 IoObject_addMethodTable_(self, methodTable);
43 return self;
46 void IoODEJoint_protoCommon(IoODEJoint *self)
48 IoObject_setDataPointer_(self, calloc(1, sizeof(IoODEJointData)));
50 JOINTID = 0;
51 JOINTGROUP = 0L;
55 IoODEJoint *IoODEJoint_rawClone(IoODEJoint *proto)
57 IoObject *self = IoObject_rawClonePrimitive(proto);
58 IoObject_setDataPointer_(self, calloc(1, sizeof(IoODEJointData)));
59 return self;
62 void IoODEJoint_free(IoODEJoint *self)
64 if(JOINTID && JOINTGROUP)
66 IoODEJointGroup_removeJoint(JOINTGROUP, self);
67 dJointDestroy(JOINTID);
69 free(IoObject_dataPointer(self));
72 void IoODEJoint_mark(IoODEJoint *self)
74 if(JOINTGROUP)
76 IoObject_shouldMark((IoObject *)JOINTGROUP);
80 IoODEJoint *IoODEJoint_newProtoCommon(void *state, IoStateProtoFunc *func, IoODEJointGroup *jointGroup)
82 IoODEJoint *proto = IoState_protoWithInitFunction_(state, func);
83 IoODEJoint *self = IOCLONE(proto);
84 JOINTGROUP = jointGroup;
85 return self;
88 /* ----------------------------------------------------------- */
90 void IoODEJoint_worldDestoryed(IoODEJoint *self)
92 JOINTGROUP = 0L;
93 JOINTID = 0;
96 /* ----------------------------------------------------------- */
99 IoObject *IoODEJoint_jointId(IoODEJoint *self, IoObject *locals, IoMessage *m)
101 return IONUMBER((long)JOINTID);
104 IoObject *IoODEJoint_jointGroup(IoODEJoint *self, IoObject *locals, IoMessage *m)
106 return JOINTGROUP ? JOINTGROUP : IONIL(self);
109 void IoODEJoint_assertValidJoint(IoODEJoint *self, IoObject *locals, IoMessage *m)
111 IOASSERT(JOINTGROUP, "This ODE joint belongs to an ODE joint group which has been freed or emptied.");
112 IOASSERT(JOINTID, "ODE Joint cannot be used directly. Clone the joint and use the Joint on the cloned joint.");
115 IoObject *IoODEJoint_attach(IoODEJoint *self, IoObject *locals, IoMessage *m)
117 dBodyID body1 = IoMessage_locals_odeBodyIdArgAt_(m, locals, 0);
118 dBodyID body2 = IoMessage_locals_odeBodyIdArgAt_(m, locals, 1);
120 IoODEJoint_assertValidJoint(self, locals, m);
121 dJointAttach(JOINTID, body1, body2);
122 return self;
125 IoObject *IoODEJoint_attachedBody1(IoODEJoint *self, IoObject *locals, IoMessage *m)
127 IoODEJoint_assertValidJoint(self, locals, m);
128 return IoODEBody_bodyFromId(IOSTATE, dJointGetBody(JOINTID, 0));
131 IoObject *IoODEJoint_attachedBody2(IoODEJoint *self, IoObject *locals, IoMessage *m)
133 IoODEJoint_assertValidJoint(self, locals, m);
134 return IoODEBody_bodyFromId(IOSTATE, dJointGetBody(JOINTID, 1));
137 IoObject *IoODEJoint_getParam(IoODEJoint *self, IoObject *locals, IoMessage *m, int parameter, IoODEJointGetParamFunc func)
139 IoODEJoint_assertValidJoint(self, locals, m);
140 return IONUMBER(func(JOINTID, parameter));
143 IoObject *IoODEJoint_setParam(IoODEJoint *self, IoObject *locals, IoMessage *m, int parameter, IoODEJointSetParamFunc func)
145 dReal value = IoMessage_locals_doubleArgAt_(m, locals, 0);
146 IoODEJoint_assertValidJoint(self, locals, m);
147 func(JOINTID, parameter, value);
148 return self;
151 IoObject *IoODEJoint_getVector3(IoODEJoint *self, IoObject *locals, IoMessage *m, IoODEJointGetVector3Func func)
153 dVector3 anchor;
155 IoODEJoint_assertValidJoint(self, locals, m);
156 func(JOINTID, anchor);
157 return IoVector_newWithODEPoint(IOSTATE, anchor);
160 IoObject *IoODEJoint_setVector3(IoODEJoint *self, IoObject *locals, IoMessage *m, IoODEJointSetVector3Func func)
162 dReal x = IoMessage_locals_doubleArgAt_(m, locals, 0);
163 dReal y = IoMessage_locals_doubleArgAt_(m, locals, 1);
164 dReal z = IoMessage_locals_doubleArgAt_(m, locals, 2);
166 IoODEJoint_assertValidJoint(self, locals, m);
167 func(JOINTID, x, y, z);
168 return self;
171 IoObject *IoODEJoint_getReal(IoODEJoint *self, IoObject *locals, IoMessage *m, IoODEJointGetReal func)
173 IoODEJoint_assertValidJoint(self, locals, m);
174 return IONUMBER(func(JOINTID));
177 IoObject *IoODEJoint_setReal(IoODEJoint *self, IoObject *locals, IoMessage *m, IoODEJointSetReal func)
179 dReal value = IoMessage_locals_doubleArgAt_(m, locals, 0);
181 IoODEJoint_assertValidJoint(self, locals, m);
182 func(JOINTID, value);
183 return self;
186 IoObject *IoODEJoint_setReal2(IoODEJoint *self, IoObject *locals, IoMessage *m, IoODEJointSetReal2 func)
188 dReal value1 = IoMessage_locals_doubleArgAt_(m, locals, 0);
189 dReal value2 = IoMessage_locals_doubleArgAt_(m, locals, 1);
191 IoODEJoint_assertValidJoint(self, locals, m);
192 func(JOINTID, value1, value2);
193 return self;