Hi,
I am trying to porting the game "Krank" http://www.youtube.com/watch?v=hnPinraICvQ which is built through PyGame.
I want to use Box2D to rebuild the effect of Snake in the video. I have check the original source for this part http://krank.sourceforge.net/
It seems that its spring only acts with one-way force while I see the most similar effect in Box2D is b2MouseJoint. And I also try to use b2DistanceJoint to emulate the effect but both two bodies in the joint are working through a two-way force.
Can anyone give an idea for this issue? Thanks.
regards,
pigling
I am trying to porting the game "Krank" http://www.youtube.com/watch?v=hnPinraICvQ which is built through PyGame.
I want to use Box2D to rebuild the effect of Snake in the video. I have check the original source for this part http://krank.sourceforge.net/
Code:
class Spring:
#-------------------------------------------------------------------------------------------
def __init__ (self, p1, p2, length=32, oneWay=1, spring=50.0, damp=5.0):
self.p1 = p1
self.p2 = p2
self.rl = length
self.ks = spring
self.kd = damp
self.oneWay = oneWay
#-------------------------------------------------------------------------------------------
def __str__ (self):
return "<%02d %02d>" % (self.p1.index, self.p2.index)
#-------------------------------------------------------------------------------------------
def calcForces(self):
v = self.p1.pos - self.p2.pos
l = v.length()
if abs(l) > 0:
fac = (self.ks*(l-self.rl)+self.kd*((self.p1.vel-self.p2.vel).dot(v))/l)/l
f = fac * v
self.p2.applyForce(f)
if not self.oneWay:
self.p1.applyForce(-f)
#-------------------------------------------------------------------------------------------
def onFrame (self, delta):
if not self.p1.player:
pointlist = [self.p1.pos, self.p2.pos]
pygame.draw.aalines(k.screen, k.level.linkColor, 1, pointlist, 1)
#-------------------------------------------------------------------------------------------
def getRect (self):
if not self.p1.player:
rect = pygame.Rect(self.p1.pos, self.p2.pos-self.p1.pos)
rect.normalize()
rect = rect.inflate(8, 8)
return rect
return pygame.Rect(0,0,0,0)
#-------------------------------------------------------------------------------------------
def __init__ (self, p1, p2, length=32, oneWay=1, spring=50.0, damp=5.0):
self.p1 = p1
self.p2 = p2
self.rl = length
self.ks = spring
self.kd = damp
self.oneWay = oneWay
#-------------------------------------------------------------------------------------------
def __str__ (self):
return "<%02d %02d>" % (self.p1.index, self.p2.index)
#-------------------------------------------------------------------------------------------
def calcForces(self):
v = self.p1.pos - self.p2.pos
l = v.length()
if abs(l) > 0:
fac = (self.ks*(l-self.rl)+self.kd*((self.p1.vel-self.p2.vel).dot(v))/l)/l
f = fac * v
self.p2.applyForce(f)
if not self.oneWay:
self.p1.applyForce(-f)
#-------------------------------------------------------------------------------------------
def onFrame (self, delta):
if not self.p1.player:
pointlist = [self.p1.pos, self.p2.pos]
pygame.draw.aalines(k.screen, k.level.linkColor, 1, pointlist, 1)
#-------------------------------------------------------------------------------------------
def getRect (self):
if not self.p1.player:
rect = pygame.Rect(self.p1.pos, self.p2.pos-self.p1.pos)
rect.normalize()
rect = rect.inflate(8, 8)
return rect
return pygame.Rect(0,0,0,0)
It seems that its spring only acts with one-way force while I see the most similar effect in Box2D is b2MouseJoint. And I also try to use b2DistanceJoint to emulate the effect but both two bodies in the joint are working through a two-way force.
Can anyone give an idea for this issue? Thanks.
regards,
pigling