public override void ProcessBlock(float deltaTime, BlockAccessor block) { var entities = block.GetEntityData(); var colliders = block.GetReadOnlyComponentData <BoxCollider>(); var hasRigidBody = block.TryGetComponentData(out Span <RigidBody> rbs); var hasScale = block.TryGetComponentData(out Span <Scale> scales); vec3 defaultScale = vec3.Ones; for (int i = 0; i < block.length; i++) { vec3 scale = hasScale ? scales[i].value : defaultScale; Box box = new Box(colliders[i].width * scale.x, colliders[i].height * scale.y, colliders[i].length * scale.z); TypedIndex shapeIdx = PhysicsSystem.Simulation.Shapes.Add(box); BodyInertia inertia = new BodyInertia(); if (hasRigidBody) { box.ComputeInertia(rbs[i].mass, out inertia); } afterUpdateCommands.AddComponent(entities[i], new InternalColliderHandle() { inertia = inertia, shapeIdx = shapeIdx }); } }
public RegularGridBuilder(Vector3 spacing, Vector3 origin, BodyInertia localInertia, TypedIndex shapeIndex = new TypedIndex()) { Spacing = spacing; Origin = origin; LocalInertia = localInertia; ShapeIndex = shapeIndex; }
public RegularGridWithKinematicBaseBuilder(Vector3 spacing, Vector3 origin, float inverseInertiaMultiplier = 0, TypedIndex shapeIndex = new TypedIndex()) { Spacing = spacing; Origin = origin; InverseInertiaMultiplier = inverseInertiaMultiplier; ShapeIndex = shapeIndex; }
public override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(-30, 8, -60); camera.Yaw = MathHelper.Pi * 3f / 4; camera.Pitch = 0; _bodyProperties = new BodyProperty <BodyProperty>(BufferPool); _events = new CollisionEvents <CollisionEventHandler>(new CollisionEventHandler(), BufferPool, ThreadDispatcher); _events.EventHandler.Pairs = new QuickList <CollidablePair>(128, BufferPool); _events.EventHandler.Simulation = Simulation; _collisionGroups = new FeeSimNarrowPhaseCallbacks <CollisionEventHandler> { Events = _events, CollisionGroups = _bodyProperties }; Simulation = Simulation.Create(BufferPool, _collisionGroups, new DefaultPoseIntegratorCallbacks(BufferPool), timestepper: new CustomPositionLastTimestepper()); var boxShape = new Box(1, 1, 1); boxShape.ComputeInertia(1, out _boxInertia); _boxIndex = Simulation.Shapes.Add(boxShape); CreateFloors(); }
// public override Vector3 Scaling // { // get => cachedScaling; // set // { // base.Scaling = value; // foreach (var colliderShape in colliderShapes) // { // colliderShape.Scaling = cachedScaling; // } // } // } internal override void CreateAndAddCollidableDescription( BepuPhysicsComponent physicsComponent, BepuSimulation xenkoSimulation, out TypedIndex shapeTypeIndex, out CollidableDescription collidableDescription) { //throw new System.NotImplementedException(); shapeTypeIndex = default; collidableDescription = default; }
public override void ProcessBlock(float deltaTime, BlockAccessor block) { var entities = block.GetEntityData(); var collider = block.GetSharedComponentData <MeshCollider>(); var hasRigidBody = block.TryGetComponentData(out Span <RigidBody> rbs); var hasScale = block.TryGetComponentData(out Span <Scale> scales); Vector3 defaultScale = Vector3.One; for (int i = 0; i < block.length; i++) { Vector3 scale = defaultScale; if (hasScale) { scale = new Vector3(scales[i].value.x, scales[i].value.y, scales[i].value.z); } Mesh mesh = new Mesh(collider.GetTriangles(), scale, PhysicsSystem.BufferPool); TypedIndex shapeIdx = PhysicsSystem.Simulation.Shapes.Add(mesh); BodyInertia inertia = new BodyInertia(); if (hasRigidBody) { mesh.ComputeOpenInertia(rbs[i].mass, out inertia); } afterUpdateCommands.AddComponent(entities[i], new InternalColliderHandle() { inertia = inertia, shapeIdx = shapeIdx }); } }
void AddShape(Shapes shapes, TypedIndex shapeIndex, ref RigidPose pose, ref Vector3 color) { switch (shapeIndex.Type) { case Sphere.Id: { SphereInstance instance; instance.Position = pose.Position; instance.Radius = shapes.GetShape <Sphere>(shapeIndex.Index).Radius; Helpers.PackOrientation(ref pose.Orientation, out instance.PackedOrientation); instance.PackedColor = Helpers.PackColor(ref color); spheres.Add(ref instance, new PassthroughArrayPool <SphereInstance>()); } break; case Capsule.Id: { CapsuleInstance instance; instance.Position = pose.Position; ref var capsule = ref shapes.GetShape <Capsule>(shapeIndex.Index); instance.Radius = capsule.Radius; instance.HalfLength = capsule.HalfLength; instance.PackedOrientation = Helpers.PackOrientationU64(ref pose.Orientation); instance.PackedColor = Helpers.PackColor(ref color); capsules.Add(ref instance, new PassthroughArrayPool <CapsuleInstance>()); } break;
internal override void CreateAndAddCollidableDescription( BepuPhysicsComponent physicsComponent, BepuSimulation xenkoSimulation, out TypedIndex shapeTypeIndex, out CollidableDescription collidableDescription) { CreateAndAddCollidableDescription <Sphere>( physicsComponent, xenkoSimulation, out shapeTypeIndex, out collidableDescription); }
internal BodyHandle CreateKinematic(Vector3 position, Quaternion rotation, TypedIndex shape) { BodyDescription bodyDescription = BodyDescription.CreateKinematic( new RigidPose(position.ToBEPU(), rotation.ToBEPU()), new BodyVelocity(new BEPUVector3(0f, 0f, 0f)), new CollidableDescription(shape, 0.1f), new BodyActivityDescription(-1)); return(simulation.Bodies.Add(bodyDescription)); }
internal BodyHandle CreateDynamic(Vector3 position, Quaternion rotation, TypedIndex shape, float mass) { simulation.Shapes.GetShape <Sphere>(shape.Index).ComputeInertia(mass, out BodyInertia inertia); BodyDescription bodyDescription = BodyDescription.CreateDynamic( new RigidPose(position.ToBEPU(), rotation.ToBEPU()), new BodyVelocity(new BEPUVector3(0f, 0f, 0f)), inertia, new CollidableDescription(shape, 0.1f), new BodyActivityDescription(-1)); return(simulation.Bodies.Add(bodyDescription)); }
public RigidBody(string name, GameObject parent = null) : base(name, parent) { dynamic = false; type = new TypedIndex(); if (parent != null) { parent.PositionChanged += OnParentOnPositionChanged; } Physics.AddBody(this); }
protected override void SetBody(TypedIndex type, float speculativeMargin, BodyInertia inertia, Vector3 offset) { var physicsSystem = GameObject.CurrentScene.GetOrCreateSystem <PhysicsSystem>(); if (_voxelStatic.Exists) { physicsSystem.RemoveStatic(_voxelStatic); } var transformedOffset = Vector3.Transform(offset, GameObject.Transform.WorldOrientation); _voxelStatic = physicsSystem.AddStatic(new StaticDescription(GameObject.Transform.WorldPosition + transformedOffset, new CollidableDescription(type, speculativeMargin)), this); }
void AddShape(Simulation simulation, TypedIndex shapeIndex, ref RigidPose pose) { switch (shapeIndex.Type) { case Sphere.Id: { SphereInstance instance; instance.Position = pose.Position; instance.Radius = simulation.Shapes.GetShape <Sphere>(shapeIndex.Index).Radius; instance.Orientation = pose.Orientation; spheres.Add(ref instance, new PassthroughArrayPool <SphereInstance>()); } break; } }
void GetPoseAndShape(CollidableReference reference, out RigidPose pose, out TypedIndex shapeIndex) { //Collidables can be associated with either bodies or statics. We have to look in a different place depending on which it is. if (reference.Mobility == CollidableMobility.Static) { var staticIndex = Simulation.Statics.HandleToIndex[reference.Handle]; pose = Simulation.Statics.Poses[staticIndex]; shapeIndex = Simulation.Statics.Collidables[staticIndex].Shape; } else { var bodyReference = Simulation.Bodies.GetBodyReference(reference.Handle); pose = bodyReference.Pose; shapeIndex = bodyReference.Collidable.Shape; } }
public void SetCollider(PhysicsType type) { if (PhysicsBodyInitialized) { RemoveCollider(); if (type == PhysicsType.None) { PhysicsType = type; if (Physics.PhysicsObjects.Contains(this)) { Physics.PhysicsObjects.Remove(this); } return; } } if (type == PhysicsType.Cube) { Box shape = new Box(2.0f, 2.0f, 2.0f); ShapeIndex = Physics.simulator.Shapes.Add(shape); Shape = shape; } else if (type == PhysicsType.Sphere) { Sphere shape = new Sphere(2.0f); ShapeIndex = Physics.simulator.Shapes.Add(shape); Shape = shape; } Shape.ComputeInertia(Mass, out BodyInertia inertia); PhysicsDescription = BodyDescription.CreateDynamic(transform.Position, inertia, new CollidableDescription(ShapeIndex, 0.1f), new BodyActivityDescription(0.01f)); BodyHandle = Physics.simulator.Bodies.Add(PhysicsDescription); BodyReference = Physics.simulator.Bodies.GetBodyReference(BodyHandle); if (!Physics.PhysicsObjects.Contains(this)) { Physics.PhysicsObjects.Add(this); } PhysicsType = type; PhysicsBodyInitialized = true; physicsEnabled = true; }
//Initialize the characters body in the physics simulation public void InitializeBody(Simulation World, Vector3 Location) { if (BodyActive) { return; } BodyActive = true; BodyShape = new Capsule(0.5f, 2); BodyIndex = World.Shapes.Add(BodyShape); CollidableDescription = new CollidableDescription(BodyIndex, 0.1f); BodyShape.ComputeInertia(1, out var Inertia); Vector3 SpawnLocation = new Vector3(Location.X, Location.Y + 1.5f, Location.Z); BodyPose = new RigidPose(SpawnLocation, Quaternion.Identity); ActivityDescription = new BodyActivityDescription(0.01f); BodyDescription = BodyDescription.CreateKinematic(BodyPose, CollidableDescription, ActivityDescription); BodyHandle = World.Bodies.Add(BodyDescription); }
protected virtual void DefinePhysicsObject(float size, float mass) { if (size == 0 || mass == 0) { throw new Exception("WorldBody defined with zero size or mass"); } var shape = new Sphere(size); var position2d = InitialPosition(); ShapeHandle = World.Simulation.Shapes.Add(shape); BodyHandle = World.Simulation.Bodies.Add(BodyDescription.CreateDynamic( new Vector3(position2d.X, 0, position2d.Y), GetBodyInertia(shape, mass), new CollidableDescription(ShapeHandle, 150f), //new CollidableDescription(ShapeHandle, 0.1f, ContinuousDetectionSettings.Continuous(1e-4f, 1e-4f)), new BodyActivityDescription(0.0f) )); }
internal Content(ContentManager contentManager) { this.contentManager = contentManager; // Efects E_BasicShader = LoadEffect("BasicShader"); E_BlinnPhong = LoadEffect("BlinnPhong"); E_LaserShader = LoadEffect("LaserShader"); E_SkyBox = LoadEffect("SkyBox"); // Models M_SkyBox = LoadModel("SkyBox/Cube", E_SkyBox); M_XWing = LoadModel("XWing/XWing", E_BlinnPhong); M_TIE = LoadModel("TIE/TIE", E_BlinnPhong); M_Trench_Plain = LoadModel("DeathStar/Trench_Plain", E_BlinnPhong); M_Trench_Line = LoadModel("DeathStar/Trench_Line", E_BlinnPhong); M_Trench_Corner = LoadModel("DeathStar/Trench_Corner", E_BlinnPhong); M_Trench_T = LoadModel("DeathStar/Trench_T", E_BlinnPhong); M_Trench_Cross = LoadModel("DeathStar/Trench_Cross", E_BlinnPhong); M_Trench_End = LoadModel("DeathStar/Trench_End", E_BlinnPhong); M_Laser = LoadModel("Laser", E_LaserShader); M_Turret = LoadModel("DeathStar/Turret", E_BlinnPhong); M_SmallTurret = LoadModel("DeathStar/SmallTurret", E_BlinnPhong); // Convex Hulls // Shapes SH_XWing = LoadConvexHull("XWing/XWing", 1f); SH_TIE = LoadShape(new Sphere(8f)); SH_Laser = LoadShape(new Cylinder(Laser.Radius / 2f, Laser.Lenght / 10f)); SH_Turret = LoadShape(new Box(28f, 66f, 28f)); SH_SmallTurret = LoadShape(new Box(1f * 10f, 2.1f * 20f, 1f * 10f)); // DeathStar shapes TypedIndex trenchPlain = LoadShape(new Box(DeathStar.trenchSize, DeathStar.trenchHeight, DeathStar.trenchSize)); TypedIndex trenchLine = LoadShape(new Box(DeathStar.trenchSize, DeathStar.trenchHeight, DeathStar.trenchSize / 1.6f)); TypedIndex trenchQuarter = LoadShape(new Box(DeathStar.trenchSize / 1.6f, DeathStar.trenchHeight, DeathStar.trenchSize / 1.6f)); RigidPose plainPose = new RigidPose(new BEPUVector3(0f, -DeathStar.trenchHeight * 1.8f, 0f)); float colliderYPos = -DeathStar.trenchHeight / 2f; float sideOffset = DeathStar.trenchSize * 3.5f / 8f; BEPUQuaternion d90Rotation = BEPUQuaternion.CreateFromAxisAngle(BEPUVector3.UnitY, (float)Math.PI / 2f); Sh_Trench_Plain = LoadKinematicCompoundShape(new (TypedIndex, RigidPose)[] {
public override void ProcessBlock(float deltaTime, BlockAccessor block) { var entities = block.GetEntityData(); var colliders = block.GetComponentData <InternalColliderHandle>(); var pos = block.GetReadOnlyComponentData <Position>(); var rot = block.GetReadOnlyComponentData <Rotation>(); for (int i = 0; i < block.length; i++) { TypedIndex shapeIdx = colliders[i].shapeIdx; Vector3 p = new Vector3(pos[i].value.x, pos[i].value.y, pos[i].value.z); BepuUtilities.Quaternion r = new BepuUtilities.Quaternion(rot[i].value.x, rot[i].value.y, rot[i].value.z, rot[i].value.w); int handle; afterUpdateCommands.AddComponent(entities[i], new InternalStaticBodyHandle() { staticBodyHandle = handle = PhysicsSystem.Simulation.Statics.Add( new StaticDescription() { Collidable = new CollidableDescription( shapeIdx, 0.1f, ContinuousDetectionSettings.Passive), Pose = new RigidPose() { Position = p, Orientation = r } } ) }); PhysicsSystem.staticBodyEntityDictionary.Add(handle, entities[i]); } }
/// <summary> /// Constructs a new collidable description. /// </summary> /// <param name="shape">Shape used by the collidable.</param> /// <param name="speculativeMargin">Radius of the margin in which to allow speculative contact generation.</param> /// <param name="continuity">Continuous collision detection settings for the collidable.</param> public CollidableDescription(TypedIndex shape, float speculativeMargin, in ContinuousDetectionSettings continuity)
internal PhysicsMesh(IShape shape, TypedIndex meshIndex) { this.Shape = shape; this.MeshIndex = meshIndex; }
public void RemoveShape(TypedIndex shapeIndex) { Simulation.Shapes.Remove(shapeIndex); _shapeContexts.Remove(shapeIndex); }
public object GetShapeContext(TypedIndex index) => _shapeContexts.ContainsKey(index) ? _shapeContexts[index] : null;
protected override void SetBody(TypedIndex type, float speculativeMargin, in BodyInertia inertia, Vector3 offset)
internal CompoundPhysicsMesh(ICompoundShape shape, TypedIndex meshIndex, BodyInertia inertia) { this.CompoundShape = shape; this.MeshIndex = meshIndex; this.Inertia = inertia; }
public void RemoveFromSimulation(PhysicsSimulation simulation) { simulation.InternalSimulation.Shapes.Remove(ShapeIndex); ShapeIndex = default; }
public T GetShape <T>(TypedIndex typedIndex) where T : unmanaged, IShape { return(Simulation.Shapes.GetShape <T>(typedIndex.Index)); }
internal StaticHandle CreateStatic(Vector3 position, Quaternion rotation, TypedIndex shape) => simulation.Statics.Add(new StaticDescription( position.ToBEPU(), rotation.ToBEPU(), new CollidableDescription(shape, 0.1f)) );
internal ConvexPhysicsMesh(IConvexShape shape, TypedIndex meshIndex) { this.ConvexShape = shape; this.MeshIndex = meshIndex; }
public SponsorNewt(Simulation simulation, TypedIndex shape, float height, in Vector2 arenaMin, in Vector2 arenaMax, Random random, int sponsorIndex) : this()