当前位置: 首页>>代码示例>>Python>>正文


Python BulletRigidBodyNode.applyForce方法代码示例

本文整理汇总了Python中panda3d.bullet.BulletRigidBodyNode.applyForce方法的典型用法代码示例。如果您正苦于以下问题:Python BulletRigidBodyNode.applyForce方法的具体用法?Python BulletRigidBodyNode.applyForce怎么用?Python BulletRigidBodyNode.applyForce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在panda3d.bullet.BulletRigidBodyNode的用法示例。


在下文中一共展示了BulletRigidBodyNode.applyForce方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SMAI

# 需要导入模块: from panda3d.bullet import BulletRigidBodyNode [as 别名]
# 或者: from panda3d.bullet.BulletRigidBodyNode import applyForce [as 别名]
class SMAI():
	#This constructs the nods tree needed for an AIChar object.
	#Takes in a model as a string, a speed double, a world object, a bullet physics world and n x, y and z positions
	def __init__(self, model, speed, world, worldNP, x, y, z):
		self.AISpeed = 80000
		self.maxSpeed = speed
		self.AIX = x
		self.AIY = y
		self.AIZ = z
		self.worldNP = worldNP
		bulletWorld = world
		self.type = ""
		self.AIModel = loader.loadModel(model)
		self.AIModel.setScale(0.5)
		self.AIModel.reparentTo(render)
		
		AIShape = BulletBoxShape(Vec3(3, 3, 1))
		self.AINode = BulletRigidBodyNode('AIChar')
		self.AINode.setMass(100)
		self.AINode.addShape(AIShape)
		
		self.AINode.setDeactivationEnabled(False)
		self.AINode.setAngularFactor(Vec3(0,0,0))
		render.attachNewNode(self.AINode)
		
		self.AIChar = self.worldNP.attachNewNode(self.AINode)
		self.AIModel.reparentTo(self.AIChar)
		self.AIChar.setPos(x, y, z)
		bulletWorld.attachRigidBody(self.AIChar.node())

	#This method needs to be called on your AIChar object to determine what type of AI you want.
	#Takes in a type string; either flee or seek and also a target object.
	def setBehavior(self, type, target):
		if(type == "flee"):
			self.type = type
			self.target = target
			print("Type set to flee")
		elif(type == "seek"):
			self.type = type
			self.target = target
			print("Type set to seek")
		else:
			print("Error @ Incorrect Type!")

	def moveTo(self, target):
		self.moveTo = True
		self.target = target
			
	def flee(self):
		h = 1
		hx = 1
		hy = 1
		if(self.AIChar.getDistance(self.target) < 40.0):
			if(self.AINode.getLinearVelocity() > self.maxSpeed):
				if(self.AINode.getLinearVelocity() < 100):
					self.AIChar.setH(self.target.getH())
					h = self.AIChar.getH()
				else:
					hx = sin(h * DEG_TO_RAD) * 10
					hy = cos(h * DEG_TO_RAD) * 10
					self.AINode.applyForce(Vec3(-hx * self.AISpeed * globalClock.getDt(), hy * self.AISpeed * globalClock.getDt(), 0), PNT)
				# else:
					# self.AINode.applyForce(Vec3(-hx * self.AISpeed * globalClock.getDt(), hy * self.AISpeed * globalClock.getDt(), 0), PNT)

	def seek(self):
		if(self.AIChar.getDistance(self.target) > 20.0):
			if(self.AINode.getLinearVelocity() > self.maxSpeed):
				self.AIChar.lookAt(self.target)
				h = self.AIChar.getH()
				hx = sin(h * DEG_TO_RAD) * 10
				hy = cos(h * DEG_TO_RAD) * 10
				self.AINode.applyForce(Vec3(-hx * self.AISpeed * globalClock.getDt(), hy * self.AISpeed * globalClock.getDt(), 0), PNT)
		
	def moveToRun(self):
		self.seek()
		# if(self.target.getX()+5 <= self.AIChar.getX() and self.target.getX()-5 >= self.AIChar.getX() and self.target.getY()+5 <= self.AIChar.getY() and self.target.getY()-5 >= self.AIChar.getY()):
			# print("It's stopped!")
			# self.AIChar.clearForces()
			# self.AIChar.setLinearVelocity(0)
		# else:
			# self.AINode.clearForces()
			# self.AIChar.lookAt(self.target)
			# self.AINode.setLinearVelocity(self.AISpeed)
	
	#Gets called on every AIChar in the world's update method to allow the AI to function at all.
	def AIUpdate(self):
		if(self.moveTo == True):
			self.moveToRun()
			
		if(self.type == "seek"):
			self.seek()
		elif(self.type == "flee"):
			self.flee()
		else:
			print("Error @ No AI Type")
开发者ID:tbackus127,项目名称:AbominableSnowmanGame,代码行数:97,代码来源:SMAI.py


注:本文中的panda3d.bullet.BulletRigidBodyNode.applyForce方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。