浏览代码

ode: fix OdeJoint.attach with None parameters

Fixes #633
rdb 6 年之前
父节点
当前提交
ca5b4e7b54
共有 5 个文件被更改,包括 70 次插入3 次删除
  1. 1 1
      panda/src/ode/odeJoint.h
  2. 18 1
      panda/src/ode/odeJoint_ext.cxx
  3. 1 1
      panda/src/ode/odeJoint_ext.h
  4. 7 0
      tests/ode/conftest.py
  5. 43 0
      tests/ode/test_ode_joints.py

+ 1 - 1
panda/src/ode/odeJoint.h

@@ -83,7 +83,7 @@ PUBLISHED:
   INLINE void set_feedback(bool flag = true);
   INLINE OdeJointFeedback *get_feedback();
 
-  EXTENSION(void attach(const OdeBody *body1, const OdeBody *body2));
+  EXTENSION(void attach(PyObject *body1, PyObject *body2));
   void attach_bodies(const OdeBody &body1, const OdeBody &body2);
   void attach_body(const OdeBody &body, int index);
   void detach();

+ 18 - 1
panda/src/ode/odeJoint_ext.cxx

@@ -29,6 +29,7 @@
 #include "odePlane2dJoint.h"
 
 #ifndef CPPPARSER
+extern Dtool_PyTypedObject Dtool_OdeBody;
 extern Dtool_PyTypedObject Dtool_OdeJoint;
 extern Dtool_PyTypedObject Dtool_OdeBallJoint;
 extern Dtool_PyTypedObject Dtool_OdeHingeJoint;
@@ -48,7 +49,23 @@ extern Dtool_PyTypedObject Dtool_OdePlane2dJoint;
  * attached to the environment.
  */
 void Extension<OdeJoint>::
-attach(const OdeBody *body1, const OdeBody *body2) {
+attach(PyObject *param1, PyObject *param2) {
+  const OdeBody *body1 = nullptr;
+  if (param1 != Py_None) {
+    body1 = (const OdeBody *)DTOOL_Call_GetPointerThisClass(param1, &Dtool_OdeBody, 1, "OdeJoint.attach", true, true);
+    if (body1 == nullptr) {
+      return;
+    }
+  }
+
+  const OdeBody *body2 = nullptr;
+  if (param2 != Py_None) {
+    body2 = (const OdeBody *)DTOOL_Call_GetPointerThisClass(param2, &Dtool_OdeBody, 2, "OdeJoint.attach", true, true);
+    if (body2 == nullptr) {
+      return;
+    }
+  }
+
   if (body1 && body2) {
     _this->attach_bodies(*body1, *body2);
 

+ 1 - 1
panda/src/ode/odeJoint_ext.h

@@ -30,7 +30,7 @@
 template<>
 class Extension<OdeJoint> : public ExtensionBase<OdeJoint> {
 public:
-  void attach(const OdeBody *body1, const OdeBody *body2);
+  void attach(PyObject *body1, PyObject *body2);
 
   PyObject *convert() const;
 };

+ 7 - 0
tests/ode/conftest.py

@@ -0,0 +1,7 @@
+import pytest
+
+
[email protected]
+def world():
+    ode = pytest.importorskip("panda3d.ode")
+    return ode.OdeWorld()

+ 43 - 0
tests/ode/test_ode_joints.py

@@ -0,0 +1,43 @@
+import pytest
+
+
+def test_odejoint_attach_both(world):
+    from panda3d import ode
+
+    body1 = ode.OdeBody(world)
+    body2 = ode.OdeBody(world)
+
+    assert len(body1.joints) == 0
+    assert len(body2.joints) == 0
+
+    joint = ode.OdeBallJoint(world)
+    joint.attach(body1, body2)
+
+    assert tuple(body1.joints) == (joint,)
+    assert tuple(body2.joints) == (joint,)
+
+
+def test_odejoint_attach_0(world):
+    from panda3d import ode
+
+    body = ode.OdeBody(world)
+
+    assert len(body.joints) == 0
+
+    joint = ode.OdeBallJoint(world)
+    joint.attach(body, None)
+
+    assert tuple(body.joints) == (joint,)
+
+
+def test_odejoint_attach_1(world):
+    from panda3d import ode
+
+    body = ode.OdeBody(world)
+
+    assert len(body.joints) == 0
+
+    joint = ode.OdeBallJoint(world)
+    joint.attach(None, body)
+
+    assert tuple(body.joints) == (joint,)