81 lines
3.0 KiB
Zig
81 lines
3.0 KiB
Zig
pub const PhysicsCollider = struct {
|
|
entity: core.Entity = undefined,
|
|
ready: bool = false,
|
|
|
|
bodyId: ?BodyId = null,
|
|
mobile: bool = false,
|
|
|
|
pub fn initECS(self: *@This(), handle: core.SetHandle) void {
|
|
self.entity = core.Entity.fromHandle(handle);
|
|
|
|
if (self.entity.get(PhysicsCharacter) != null) {
|
|
@panic("adding a physics collider to an entity already controlled by physics character. I don't want to support this");
|
|
}
|
|
|
|
if (self.entity.fetch(core.Scene) == null) {
|
|
@panic("a scene component needs to be created in order to add a physics collider");
|
|
}
|
|
}
|
|
|
|
pub fn setupShape(self: *@This(), name: []const u8) void {
|
|
_ = self;
|
|
_ = name;
|
|
}
|
|
|
|
pub fn setupByShapeName(self: *@This(), shapeName: core.Name, bodySettings: zphysics.BodyCreationSettings) !void {
|
|
var n = shapeName;
|
|
const system = physics.context();
|
|
const interface = system.system.getBodyInterfaceMut();
|
|
const shape = system.shapes.get(n.handle()).?;
|
|
var settings = bodySettings;
|
|
|
|
settings.shape = shape.shape;
|
|
|
|
const scene = self.entity.fetch(core.Scene).?;
|
|
const p = scene.getPosition();
|
|
// core.engine_log("setting up collider shape {any}", .{p});
|
|
settings.position = .{ p.x, p.y, p.z, 1.0 };
|
|
settings.rotation = scene.getRotation().quat;
|
|
|
|
self.bodyId = try interface.createAndAddBody(settings, .activate);
|
|
self.mobile = settings.motion_type == .dynamic;
|
|
self.ready = true;
|
|
|
|
try system.registerBodyEntity(self.bodyId.?, self.entity);
|
|
}
|
|
|
|
pub fn applyScene(self: *@This()) void {
|
|
const scene = self.entity.get(core.Scene).?;
|
|
if (scene.getParent().handle.alive != false) {
|
|
@panic("trying to apply scene but it has a parent. physics bodies do not support having parents... yet");
|
|
}
|
|
physics.setBodyPosition(self.bodyId, scene.getPosition());
|
|
physics.setRotation(self.bodyId, scene.getRotation());
|
|
}
|
|
|
|
pub fn deinitECS(self: *@This(), handle: core.SetHandle) void {
|
|
_ = handle;
|
|
core.engine_log("destroy body id {any}", .{self.bodyId.?});
|
|
physics.context().pushDestroyBodyId(self.bodyId.?);
|
|
}
|
|
|
|
pub fn deinit(self: *@This()) void {
|
|
_ = self;
|
|
// todo.
|
|
// try system.bodyIdToEntityMap.remove(self.bodyId);
|
|
// system.destroy entity
|
|
//try physics.context().idToEntity.remove(self.bodyId.?);
|
|
}
|
|
|
|
pub var BaseContainer: *core.SparseMap(PhysicsCollider) = undefined;
|
|
pub const ComponentName = "PhysicsCollider";
|
|
pub const ScriptExports: []const []const u8 = &.{};
|
|
};
|
|
|
|
const core = @import("core");
|
|
const zphysics = @import("zphysics");
|
|
const physicsSystem = @import("physicsSystem.zig");
|
|
const PhysicsCharacter = physicsSystem.PhysicsCharacter;
|
|
const physics = @import("physics.zig");
|
|
const BodyId = physics.BodyId;
|