You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Bullet-for-ursina碰撞器在Ursina中无法正常工作

Ursina + Bullet-for-ursina 物理碰撞问题修复

针对你遇到的碰撞器偏移、实体穿透、卡顿问题,直接给你排查后的修复方案:

核心问题点与修复步骤

  1. 实体创建顺序错误
    Ursina的所有Entity必须在app=Ursina()之后创建,否则会导致模型坐标、内部状态初始化异常,这是你碰撞器位置偏移的主要原因。把enemy的创建代码移到app=Ursina()之后。

  2. 双重碰撞器冲突
    你给地面同时设置了Ursina内置的collider="box"和Bullet的BoxCollider,两种碰撞系统会互相干扰,直接移除Ursina内置的collider配置。

  3. 碰撞器质量设置错误
    静态物体(比如地面)必须设置mass=0,否则会被重力影响或出现碰撞异常;动态物体(比如敌人)设置mass>0才能受重力作用下落。

  4. 碰撞器类型选择
    对于立方体这类规则形状,用BoxCollider比MeshCollider效率高得多,能解决卡顿问题,同时碰撞匹配更精准。

修改后的完整代码

from ursina import *
from physics3d import *
from panda3d.bullet import BulletWorld
from ursina.prefabs.first_person_controller import FirstPersonController

# 必须先初始化Ursina app
app = Ursina()

ground = Entity(
    model="cube",
    scale=(200, 5, 200),
    texture="grass",
    position=(0, -22, 0),
    name="floor"
    # 移除Ursina内置collider,改用Bullet碰撞器
)

enemy = Entity(position=(10, -8, -15), scale=(1, 1, 1), model="cube")

character = Entity(model="cube", rotation_y=90, name="player")
player = FirstPersonController(model=character, origin=(0, -0.5, 0))

# 初始化Bullet物理世界
world = BulletWorld()
Debugger(world, wireframe=True)
world.setGravity(Vec3(0, -9.81, 0))

# 给敌人添加动态BoxCollider(mass=1)
BoxCollider(world, enemy, mass=1)
# 给地面添加静态BoxCollider(mass=0)
BoxCollider(world, ground, mass=0)

def update():
    dt = time.dt
    world.doPhysics(dt)

app.run()

额外注意事项

  • 如果你坚持用MeshCollider,确保模型的原点是中心位置,Ursina的cube模型默认原点是中心,所以没问题,但MeshCollider性能远不如BoxCollider,不推荐用于规则形状。
  • 调试碰撞器时,Debugger显示的线框如果还是有偏移,检查实体的origin设置,避免给动态实体设置非中心的origin,否则会导致碰撞器和模型错位。

内容的提问来源于stack exchange,提问作者Beckett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:34:56