1 Star 0 Fork 0

EricCheng/Simple Application of Potential Functions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Agent.py 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
import math
import pygame
import Controller as con
vector = pygame.Vector2
# 定义角色类
class Agent(pygame.sprite.Sprite):
def __init__(self, mass,inertia,pos:vector, vel:vector,radius=10,color=(255,0,0)):
super().__init__()
self.fMass = mass
self.fInertia = inertia
self.fInertiaInverse = 1/self.fInertia
self.vPostion = pos
self.vVelocity = vel
self.fSpeed = vel.length()
self.vForces = vector(0, 0)
self.vMoveForce = vector(0,0)
self.radius = radius
self.color = color
self.image = pygame.Surface((self.radius * 2, self.radius * 2))
self.image.set_colorkey((0, 0, 0)) #将图像的黑色部分设置为透明。
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.rect = self.image.get_rect()
self.rect.x = self.vPostion.x - self.radius
self.rect.y = self.vPostion.y - self.radius
# 更新速度和位置
def update(self, force:vector):
self.vForces = force + self.vMoveForce
self.vVelocity += self.vForces / self.fMass
self.rigid_resist()
self.vPostion += self.vVelocity
self.rect.x = self.vPostion.x - self.radius
self.rect.y = self.vPostion.y - self.radius
def rigid_calc_resist(self, mode: int):
if (mode == -1):
if (self.vVelocity.x > 0): # 阻力计算
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * math.sqrt(abs(self.vVelocity.x))
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x + self.fInertiaInverse * math.sqrt(abs(self.vVelocity.x))
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * math.sqrt(abs(self.vVelocity.y))
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y + self.fInertiaInverse * math.sqrt(abs(self.vVelocity.y))
if (mode == 0):
if (self.vVelocity.x > 0): # 阻力计算
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y
if (mode == 1):
if (self.vVelocity.x > 0): # 阻力计算
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x ** 2
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x + self.fInertiaInverse * self.vVelocity.x ** 2
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y ** 2
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y + self.fInertiaInverse * self.vVelocity.y ** 2
if (mode == 2): # 如果速度小于1,反而会很难停下来
if (self.vVelocity.x > 0): # 注意符号
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x ** 3
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x ** 3
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y ** 3
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y ** 3
def rigid_resist(self):
if self.fSpeed > 30:
self.rigid_calc_resist(2)
elif self.fSpeed >= 5:
self.rigid_calc_resist(1)
elif self.fSpeed >= 0.5:
self.rigid_calc_resist(0)
else:
self.rigid_calc_resist(-1)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ericchengscut/simple-application-of-potential-functions.git
git@gitee.com:ericchengscut/simple-application-of-potential-functions.git
ericchengscut
simple-application-of-potential-functions
Simple Application of Potential Functions
master

搜索帮助