def fromPlaneAndRadius(plane, radius = 32768): normal = plane.getNormal() dist = -plane.getW() # Find the major axis x = plane.getClosestAxisToNormal() up = Vec3.unitX() if x == Vec3.unitZ() else Vec3.unitZ() v = up.dot(normal) up = CIGlobals.extrude(up, -v, normal) up.normalize() org = normal * dist right = up.cross(normal) up = up * radius right = right * radius # Project a really big axis aligned box onto the plane verts = [ org - right + up, org + right + up, org + right - up, org - right - up ] poly = Winding(verts, plane) return poly
def setAction(self, action): BaseGagAI.setAction(self, action) if action == self.StateThrow: if not isinstance(self.avatar, BaseNPCAI): self.takeAmmo(-1) throwVector = PhysicsUtils.getThrowVector( self.avatar.getViewOrigin(), self.traceVector, self.avatar.getViewOrigin(), self.avatar, self.avatar.getBattleZone().getPhysicsWorld()) endPos = CIGlobals.extrude(self.avatar.getViewOrigin(), self.ThrowPower, throwVector) - (0, 0, 90) proj = WholeCreamPieProjectileAI(base.air) proj.setProjectile(2.5, self.avatar.getViewOrigin(), endPos, 1.07, globalClockDelta.getFrameNetworkTime()) proj.generateWithRequired(self.avatar.getBattleZone().zoneId) proj.addHitCallback(self.onProjectileHit) proj.addExclusion(self.avatar) self.avatar.emitSound(SOUND_COMBAT, self.avatar.getViewOrigin(), duration=0.5) self.throwTime = globalClock.getFrameTime()
def throwGearsAtTarget(self): if not self.target: return startPos = self.getPos(render) + (0, 0, 17) targetPos = self.target.entity.getViewOrigin() toTarget = (targetPos - startPos).normalized() endPos = CIGlobals.extrude(startPos, 100, toTarget) gear = GearAI(self.air) gear.setLinear(2.5, startPos, endPos, globalClockDelta.getFrameNetworkTime()) gear.generateWithRequired(self.getBattleZone().zoneId) gear.addHitCallback(self.onGearHit) gear.addExclusion(self) self.emitSound(SOUND_COMBAT, self.getViewOrigin(), duration=0.5)
def think(self): BaseHitscanAI.think(self) if self.action == self.StateFire: if self.isAIUser(): if CIGlobals.isNodePathOk(self.target): self.avatar.headsUp(self.target) self.calibrate(self.target) else: return if (self.gumballsToFire > 0 and globalClock.getFrameTime() >= self.nextFireTime): throwVector = PhysicsUtils.getThrowVector( self.traceOrigin, self.traceVector, self.fireOrigin, self.avatar, self.avatar.getBattleZone().getPhysicsWorld()) endPos = CIGlobals.extrude(self.fireOrigin, self.FirePower, throwVector) - (0, 0, 90) if self.isAIUser(): endPos = endPos + Point3(random.uniform(-2.5, 2.5), random.uniform(-2.5, 2.5), random.uniform(-2.5, 2.5)) self.takeAmmo(-1) self.fireProjectile(endPos) self.gumballsToFire = self.gumballsToFire - 1 if (self.gumballsToFire > 0): self.nextFireTime = globalClock.getFrameTime( ) + random.uniform(self.MIN_BURST_DELAY, self.MAX_BURST_DELAY)
def __updateTask(self, task): # TODO -- This function does a lot of math, I measured it to take .5 ms on my laptop # That's a lot of time for something miniscule like sway and bob. dt = globalClock.getDt() time = globalClock.getFrameTime() if base.localAvatar.isFirstPerson(): eyePoint = base.localAvatar.getEyePoint() if base.localAvatar.walkControls.crouching: eyePoint[2] = eyePoint[2] / 2.0 eyePoint[2] = CIGlobals.lerpWithRatio(eyePoint[2], self.lastEyeHeight, 0.4) self.lastEyeHeight = eyePoint[2] camRootAngles = Vec3(0) # Mouse look around mw = base.mouseWatcherNode if mw.hasMouse(): md = base.win.getPointer(0) center = Point2(base.win.getXSize() / 2, base.win.getYSize() / 2) xDist = md.getX() - center.getX() yDist = md.getY() - center.getY() sens = self.__getMouseSensitivity() angular = -(xDist * sens) / dt base.localAvatar.walkControls.controller.setAngularMovement( angular) camRootAngles.setY(self.lastPitch - yDist * sens) if camRootAngles.getY() > FPSCamera.MaxP: camRootAngles.setY(FPSCamera.MaxP) yDist = 0 elif camRootAngles.getY() < FPSCamera.MinP: yDist = 0 camRootAngles.setY(FPSCamera.MinP) base.win.movePointer(0, int(center.getX()), int(center.getY())) if base.localAvatar.isFirstPerson(): # Camera / viewmodel bobbing vmBob = Point3(0) vmAngles = Vec3(0) vmRaise = Point3(0) camBob = Point3(0) maxSpeed = base.localAvatar.walkControls.BattleRunSpeed * 16.0 speed = base.localAvatar.walkControls.speeds.length() * 16.0 speed = max(-maxSpeed, min(maxSpeed, speed)) bobOffset = CIGlobals.remapVal(speed, 0, maxSpeed, 0.0, 1.0) self.bobTime += (time - self.lastBobTime) * bobOffset self.lastBobTime = time # Calculate the vertical bob cycle = self.bobTime - int( self.bobTime / self.BobCycleMax) * self.BobCycleMax cycle /= self.BobCycleMax if cycle < self.BobUp: cycle = math.pi * cycle / self.BobUp else: cycle = math.pi + math.pi * (cycle - self.BobUp) / (1.0 - self.BobUp) verticalBob = speed * 0.005 verticalBob = verticalBob * 0.3 + verticalBob * 0.7 * math.sin( cycle) verticalBob = max(-7.0, min(4.0, verticalBob)) verticalBob /= 16.0 # Calculate the lateral bob cycle = self.bobTime - int( self.bobTime / self.BobCycleMax * 2) * self.BobCycleMax * 2 cycle /= self.BobCycleMax * 2 if cycle < self.BobUp: cycle = math.pi * cycle / self.BobUp else: cycle = math.pi + math.pi * (cycle - self.BobUp) / (1.0 - self.BobUp) lateralBob = speed * 0.005 lateralBob = lateralBob * 0.3 + lateralBob * 0.7 * math.sin(cycle) lateralBob = max(-7.0, min(4.0, lateralBob)) lateralBob /= 16.0 # Apply bob, but scaled down a bit vmBob.set(lateralBob * 0.8, 0, verticalBob * 0.1) # Z bob a bit more vmBob[2] += verticalBob * 0.1 # Bob the angles vmAngles[2] += verticalBob * 0.5 vmAngles[1] -= verticalBob * 0.4 vmAngles[0] -= lateralBob * 0.3 # ================================================================ # Viewmodel lag/sway angles = self.camRoot.getHpr(render) quat = Quat() quat.setHpr(angles) invQuat = Quat() invQuat.invertFrom(quat) maxVMLag = 1.5 lagforward = quat.getForward() if dt != 0.0: lagdifference = lagforward - self.lastFacing lagspeed = 5.0 lagdiff = lagdifference.length() if (lagdiff > maxVMLag) and (maxVMLag > 0.0): lagscale = lagdiff / maxVMLag lagspeed *= lagscale self.lastFacing = CIGlobals.extrude(self.lastFacing, lagspeed * dt, lagdifference) self.lastFacing.normalize() lfLocal = invQuat.xform(lagdifference) vmBob = CIGlobals.extrude(vmBob, 5.0 / 16.0, lfLocal * -1.0) pitch = angles[1] if pitch > 180: pitch -= 360 elif pitch < -180: pitch += 360 vmBob = CIGlobals.extrude(vmBob, pitch * (0.035 / 16), Vec3.forward()) vmBob = CIGlobals.extrude(vmBob, pitch * (0.03 / 16), Vec3.right()) vmBob = CIGlobals.extrude(vmBob, pitch * (0.02 / 16), Vec3.up()) # ================================================================ vmRaise.set( 0, 0, 0 ) #(0, abs(camRootAngles.getY()) * -0.002, camRootAngles.getY() * 0.002) camBob.set(0, 0, 0) # Apply bob, raise, and sway to the viewmodel. self.viewModel.setPos(vmBob + vmRaise + self.lastVMPos) self.vmRoot2.setHpr(vmAngles) self.camRoot.setPos(eyePoint + camBob) newPitch = camRootAngles.getY() if abs(newPitch - self.lastPitch) > self.PitchUpdateEpsilon: # Broadcast where our head is looking head = base.localAvatar.getPart("head") if head and not head.isEmpty(): # Constrain the head pitch a little bit so it doesn't look like their head snapped headPitch = max(-47, newPitch) headPitch = min(75, headPitch) base.localAvatar.b_setLookPitch(headPitch) self.lastPitch = newPitch if base.localAvatar.isFirstPerson(): # Apply punch angle self.decayPunchAngle() camRootAngles += self.punchAngle self.camRoot.setHpr(camRootAngles) return task.cont