Ticket #923: Hinge.pym

File Hinge.pym, 3.6 KB (added by SaraKazemi, 2 years ago)
Line 
1# PySoy joints.Hinge Class
2#
3# Copyright (C) 2006,2007,2008,2009 PySoy Group
4#
5#  This program is free software; you can redistribute it and/or modify
6#  it under the terms of the GNU Affero General Public License as published
7#  by the Free Software Foundation, either version 3 of the License, or
8#  (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU Affero General Public License for more details.
14#
15#  You should have received a copy of the GNU Affero General Public License
16#  along with this program; if not, see http://www.gnu.org/licenses
17#
18# $Id: Hinge.pym 1393 2008-12-31 23:51:25Z ArcRiley $
19
20cdef class Hinge (Joint) :
21  '''PySoy Hinge Joint
22
23    a hinge :class:`~soy.joints.Joint`
24  '''
25
26  ############################################################################
27  #
28  # Python functions
29  #
30 
31  def __init__(self, parent, body1, body2, position, axis) :
32    self._scene._stepLock()
33    self._jointID = ode.dJointCreateHinge(self._scene._worldID, NULL)
34    ode.dJointAttach(self._jointID, self._body1._bodyID, self._body2._bodyID)
35    self.position = position
36    self.axis = axis
37    self._scene._joints._append(<void*> self)
38    self._scene._stepUnLock()
39
40  def addTorque(self, torque) :
41        '''Hinge.addTorque
42       
43        this applies torque to the hinge joint
44        '''
45    ode.dJointAddHingeTorque(self._jointID, torque)
46
47  ############################################################################
48  #
49  # Properties
50  #
51 
52  property position :
53    '''Hinge.position
54   
55    this is the current position of the joint's anchor
56    '''
57    def __set__(self, anchor) :
58      ode.dJointSetHingeAnchor(self._jointID, anchor[0], anchor[1], anchor[2])
59    def __get__(self) :
60      cdef ode.dVector3 anchor
61      ode.dJointGetHingeAnchor(self._jointID, anchor)
62      return (anchor[0], anchor[1], anchor[2])
63 
64  property axis :
65    '''Hinge.axis
66
67    this is the axis of the hinge
68    '''
69    def __set__(self, axis) :
70      ode.dJointSetHingeAxis(self._jointID, axis[0], axis[1], axis[2])
71    def __get__(self) :
72      cdef ode.dVector3 axis
73      ode.dJointGetHingeAxis(self._jointID, axis)
74      return (axis[0], axis[1], axis[2])
75
76  property angle :
77    '''Hinge.angle
78
79    this is the angle of the hinge
80    '''
81    def __get__(self) :
82      return ode.dJointGetHingeAngle(self._jointID)
83
84  property stops :
85    '''Hinge.stops
86
87    this is the minimum and maximum angle of the hinge
88    '''
89    def __set__(self, angle) :
90      ode.dJointSetHingeParam(self._jointID, ode.dParamLoStop, angle[0])
91      ode.dJointSetHingeParam(self._jointID, ode.dParamHiStop, angle[1])
92    def __get__(self) :
93      cdef ode.dReal loangle
94      cdef ode.dReal hiangle
95      loangle = ode.dJointGetHingeParam(self._jointID, ode.dParamLoStop)
96      hiangle = ode.dJointGetHingeParam(self._jointID, ode.dParamHiStop)
97      if loangle < -3.1416 or hiangle > 3.1416 :
98        return None
99      else :
100        return (loangle, hiangle)
101    def __del__(self) :
102      ode.dJointSetHingeParam(self._jointID, ode.dParamLoStop, -ode.dInfinity)
103      ode.dJointSetHingeParam(self._jointID, ode.dParamHiStop,  ode.dInfinity)
104
105  property bounce :
106    '''Hinge.bounce
107
108    bounciness of stops, between 0.0 and 1.0
109    '''
110    def __set__(self, bounce) :
111      if bounce < 0.0 or bounce > 1.0 :
112        raise(ValueError('0.0 to 1.0 only'))
113      ode.dJointSetHingeParam(self._jointID, ode.dParamBounce, bounce)
114    def __get__(self) :
115      return ode.dJointGetHingeParam(self._jointID, ode.dParamBounce)
116