1 个不稳定版本
| 0.1.0 | 2022年5月9日 | 
|---|
#1588 在 游戏开发
26KB
276 行
但斯里
一个物理引擎 一个提供2D碰撞检测和运动学的资产,在bevy中从头构建
如何使用
将PhysicsPlugin添加到程序中,并可选地添加DebugPlugin以查看实体的碰撞。
App::new()
	.add_plugins(DefaultPlugins)
	.add_plugin(PhysicsPlugin::default())
	// This plugin allows us to see the collisions
	//.add_plugin(DebugPlugin)
然后创建一个至少包含Transform组件的实体后,插入CollisionShape组件以检测碰撞以及与之碰撞的对象,如果需要,还可以插入具有物理属性(如速度、加速度或力量)的KinematicBundle
commands.spawn_bundle(SpriteBundle {
	sprite: Sprite {
		custom_size: Some(Vec2::splat(100.)),
		color: Color::CYAN,
		..Default::default()
	},
	..Default::default()
})
// The size you pass to ColliderShape::AABB(_, _) should be half what you want
.insert(ColliderShape::AABB(50., 50.));
最后应该类似于以下示例
use bevy::prelude::*;
use butsuri::prelude::*;
fn main() {
	App::new()
		.add_plugins(DefaultPlugins)
		.add_plugin(PhysicsPlugin::default())
		.add_system(setup)
	.run();
}
fn setup(mut commands: Commands) {
	commands.spawn_bundle(SpriteBundle {
		sprite: Sprite {
			custom_size: Some(Vec2::splat(100.)),
			color: Color::CYAN,
			..Default::default()
		},
		..Default::default()
	})
	.insert(ColliderShape::AABB(50., 50.));
}
// handle collisions as you want
fn collision(
	mut collision_event: EventReader<CollisionEvent>,
	mut query: Query<&mut Sprite>,
) {
	// ...
}
示例
与其他类似项目相比,这个项目有多有用?
很少,因为有更好的选择提供更多功能,例如 heron 或 bevy_rapier。我进行这个项目是为了了解物理引擎的内部工作原理,我在动量等方面有些迷茫,但我会继续更新这个项目以扩展我的知识,我欢迎批评或建议。
如果您想联系我,可以通过Bevy服务器上的discord(NemuiSen#4114)进行
PS:如果有些东西听起来不太对劲,我是在用我对英语的模糊了解和谷歌翻译来写这篇文章的。
依赖项
~39–53MB
~778K SLoC