golf: update physics simulations

This commit is contained in:
John Cote 2020-01-07 22:40:15 -05:00
parent efc2147ad0
commit f73acc5598
2 changed files with 22 additions and 19 deletions

View file

@ -209,7 +209,7 @@ class GolfHoleBase:
frameCount = 0 frameCount = 0
lift = 0 lift = 0
startTime = GolfGlobals.BALL_CONTACT_FRAME / 24 startTime = GolfGlobals.BALL_CONTACT_FRAME / 24
startFrame = startTime * self.FPS startFrame = int(startTime * self.FPS)
for frame in range(startFrame): for frame in range(startFrame):
self.simulate() self.simulate()
self.setTimeIntoCycle(self.swingTime + float(frameCount) * self.DTAStep) self.setTimeIntoCycle(self.swingTime + float(frameCount) * self.DTAStep)
@ -334,14 +334,13 @@ class GolfHoleBase:
self.ballRayBody.setPosition(bp[0], bp[1], bp[2]) self.ballRayBody.setPosition(bp[0], bp[1], bp[2])
self.skyRay.setPosition(bp[0], bp[1], 50.0) self.skyRay.setPosition(bp[0], bp[1], 50.0)
def getOrderedContacts(self, count): def getOrderedContacts(self, entry):
c0 = self.space.getContactId(count, 0) c0 = self.space.getCollideId(entry.getGeom1())
c1 = self.space.getContactId(count, 1) c1 = self.space.getCollideId(entry.getGeom2())
if c0 > c1: if c0 > c1:
chold = c1 return c1, c0
c1 = c0 else:
c0 = chold return c0, c1
return (c0, c1)
def postStep(self): def postStep(self):
if self.canRender: if self.canRender:
@ -352,11 +351,9 @@ class GolfHoleBase:
skyRayHitPos = None skyRayHitPos = None
ballRayHitPos = None ballRayHitPos = None
bp = self.curGolfBall().getPosition() bp = self.curGolfBall().getPosition()
for count in range(self.colCount): for entry in self.colEntries:
c0, c1 = self.getOrderedContacts(count) c0, c1 = self.getOrderedContacts(entry)
x = self.space.getContactData(count * 3 + 0) x, y, z = entry.getContactPoint(0)
y = self.space.getContactData(count * 3 + 1)
z = self.space.getContactData(count * 3 + 2)
if c0 == GolfGlobals.OOB_RAY_COLLIDE_ID or c1 == GolfGlobals.OOB_RAY_COLLIDE_ID: if c0 == GolfGlobals.OOB_RAY_COLLIDE_ID or c1 == GolfGlobals.OOB_RAY_COLLIDE_ID:
rayCount += 1 rayCount += 1
if self.canRender: if self.canRender:
@ -382,10 +379,9 @@ class GolfHoleBase:
self.greenIn = self.frame self.greenIn = self.frame
self.llv = self.curGolfBall().getLinearVel() self.llv = self.curGolfBall().getLinearVel()
elif GolfGlobals.BALL_COLLIDE_ID in [c0, c1] and GolfGlobals.HOLE_CUP_COLLIDE_ID in [c0, c1]: elif GolfGlobals.BALL_COLLIDE_ID in [c0, c1] and GolfGlobals.HOLE_CUP_COLLIDE_ID in [c0, c1]:
zCon = self.space.getContactData(count * 3 + 2)
self.ballTouchedHoleFrame = self.frame self.ballTouchedHoleFrame = self.frame
ballUndersideZ = self.curGolfBall().getPosition()[2] - 0.05 ballUndersideZ = self.curGolfBall().getPosition()[2] - 0.05
if zCon < ballUndersideZ: if z < ballUndersideZ:
if not self.ballInHoleFrame: if not self.ballInHoleFrame:
self.ballInHoleFrame = self.frame self.ballInHoleFrame = self.frame
if self.ballFirstTouchedHoleFrame < self.ballLastTouchedGrass: if self.ballFirstTouchedHoleFrame < self.ballLastTouchedGrass:
@ -406,9 +402,6 @@ class GolfHoleBase:
self.ballLastTouchedGrass = self.frame self.ballLastTouchedGrass = self.frame
elif self.canRender: elif self.canRender:
if c0 == GolfGlobals.TOON_RAY_COLLIDE_ID or c1 == GolfGlobals.TOON_RAY_COLLIDE_ID: if c0 == GolfGlobals.TOON_RAY_COLLIDE_ID or c1 == GolfGlobals.TOON_RAY_COLLIDE_ID:
x = self.space.getContactData(count * 3 + 0)
y = self.space.getContactData(count * 3 + 1)
z = self.space.getContactData(count * 3 + 2)
self.toonRayCollisionCallback(x, y, z) self.toonRayCollisionCallback(x, y, z)
if GolfGlobals.CAMERA_RAY_COLLIDE_ID in [c0, c1] and GolfGlobals.WINDMILL_BASE_COLLIDE_ID in [c0, c1]: if GolfGlobals.CAMERA_RAY_COLLIDE_ID in [c0, c1] and GolfGlobals.WINDMILL_BASE_COLLIDE_ID in [c0, c1]:
self.translucentCurFrame.append(self.windmillFanNodePath) self.translucentCurFrame.append(self.windmillFanNodePath)

View file

@ -60,6 +60,9 @@ class PhysicsWorldBase:
self.refFPS = 60.0 self.refFPS = 60.0
self.DTAStep = 1.0 / self.FPS self.DTAStep = 1.0 / self.FPS
self.refCon = 1.2 self.refCon = 1.2
self.collisionEventName = 'ode-collision-%s' % id(self)
self.space.setCollisionEvent(self.collisionEventName)
self.accept(self.collisionEventName, self.__handleCollision)
def delete(self): def delete(self):
self.notify.debug('Max Collision Count was %s' % self.maxColCount) self.notify.debug('Max Collision Count was %s' % self.maxColCount)
@ -106,6 +109,7 @@ class PhysicsWorldBase:
self.space.destroy() self.space.destroy()
self.world = None self.world = None
self.space = None self.space = None
self.ignore(self.collisionEventName)
return return
def setupSimulation(self): def setupSimulation(self):
@ -194,8 +198,14 @@ class PhysicsWorldBase:
endTime = globalClock.getRealTime() - startTime endTime = globalClock.getRealTime() - startTime
return task.cont return task.cont
def __handleCollision(self, entry):
self.colEntries.append(entry)
def simulate(self): def simulate(self):
self.colCount = self.space.autoCollide() self.colEntries = []
self.space.autoCollide()
eventMgr.doEvents()
self.colCount = len(self.colEntries)
if self.maxColCount < self.colCount: if self.maxColCount < self.colCount:
self.maxColCount = self.colCount self.maxColCount = self.colCount
self.notify.debug('New Max Collision Count %s' % self.maxColCount) self.notify.debug('New Max Collision Count %s' % self.maxColCount)