Example #1
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var players           = block.GetReadOnlyComponentData <Player>();
            var rotations         = block.GetReadOnlyComponentData <Rotation>();
            var velocities        = block.GetComponentData <Velocity>();
            var angularVelocities = block.GetComponentData <AngularVelocity>();

            for (int i = 0; i < block.length; i++)
            {
                if (Input.GetKey(Key.W))
                {
                    var dir = rotations[i].value * vec3.UnitY;
                    velocities[i].value += dir * deltaTime * players[i].movementSpeed;
                }

                if (Input.GetKey(Key.A))
                {
                    angularVelocities[i].value -= vec3.UnitZ * deltaTime * players[i].turningSpeed;
                }

                if (Input.GetKey(Key.D))
                {
                    angularVelocities[i].value += vec3.UnitZ * deltaTime * players[i].turningSpeed;
                }
            }
        }
Example #2
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var rbs  = block.GetComponentData <RigidBody>();
            var rbh  = block.GetReadOnlyComponentData <InternalRigidBodyHandle>();
            var pos  = block.GetComponentData <Position>();
            var rot  = block.GetComponentData <Rotation>();
            var vel  = block.GetComponentData <Velocity>();
            var avel = block.GetComponentData <AngularVelocity>();

            for (int i = 0; i < block.length; i++)
            {
                var reference = PhysicsSystem.Simulation.Bodies.GetBodyReference(rbh[i].rigidBodyHandle);
                var p         = reference.Pose.Position;
                var r         = reference.Pose.Orientation;
                var lv        = reference.Velocity.Linear;
                var av        = reference.Velocity.Angular;


                pos[i].value  = new vec3(p.X, p.Y, p.Z);
                rot[i].value  = new quat(r.X, r.Y, r.Z, r.W);
                vel[i].value  = new vec3(lv.X, lv.Y, lv.Z);
                avel[i].value = new vec3(av.X, av.Y, av.Z);

                rbs[i].lastPosition   = pos[i].value;
                rbs[i].lastRotation   = rot[i].value;
                rbs[i].lastLinearVel  = vel[i].value;
                rbs[i].lastAngularVel = avel[i].value;
            }
        }
Example #3
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block, ECSWorld world)
        {
            var positions = block.GetComponentData <Position>();
            var follows   = block.GetReadOnlyComponentData <FollowTarget>();
            var rotations = block.GetComponentData <Rotation>();

            for (int i = 0; i < block.length; i++)
            {
                if (!world.ComponentManager.TryGetComponent(follows[i].target, out Position targetPos))
                {
                    continue;
                }
                if (!world.ComponentManager.TryGetComponent(follows[i].target, out Rotation targetRot))
                {
                    targetRot = new Rotation();
                }

                var p    = targetPos.value + follows[i].offset;
                var rOff = new quat(follows[i].angleOffset);
                var r    = targetRot.value * rOff;

                positions[i].value = p;
                rotations[i].value = r;
            }
        }
Example #4
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var positions         = block.GetComponentData <Position>();
            var velocities        = block.GetComponentData <Velocity>();
            var rotations         = block.GetComponentData <Rotation>();
            var angularVelocities = block.GetComponentData <AngularVelocity>();
            var values            = block.GetReadOnlyComponentData <PhysicsBodyLockedValues>();
            var locked            = block.GetReadOnlyComponentData <PhysicsBodyLockAxis>();

            for (int i = 0; i < block.length; i++)
            {
                var newPosition = positions[i].value;
                var newVelocity = velocities[i].value;
                if (locked[i].lockX)
                {
                    newPosition.x = values[i].pos.x;
                    newVelocity.x = 0;
                }
                if (locked[i].lockY)
                {
                    newPosition.y = values[i].pos.y;
                    newVelocity.y = 0;
                }
                if (locked[i].lockZ)
                {
                    newPosition.z = values[i].pos.z;
                    newVelocity.z = 0;
                }

                var newRot    = (vec3)rotations[i].value.EulerAngles;
                var oldRot    = (vec3)values[i].rot.EulerAngles;
                var newRotVel = angularVelocities[i].value;

                if (locked[i].lockRotX)
                {
                    newRot.x    = oldRot.x;
                    newRotVel.x = 0;
                }
                if (locked[i].lockRotY)
                {
                    newRot.y    = oldRot.y;
                    newRotVel.y = 0;
                }
                if (locked[i].lockRotZ)
                {
                    newRot.z    = oldRot.z;
                    newRotVel.z = 0;
                }

                positions[i].value         = newPosition;
                rotations[i].value         = new quat(newRot);
                velocities[i].value        = newVelocity;
                angularVelocities[i].value = newRotVel;
            }
        }
Example #5
0
            public override void ProcessBlock(float deltaTime, BlockAccessor block)
            {
                var positions = block.GetComponentData <Position>();
                var rotations = block.GetComponentData <Rotation>();

                for (int i = 0; i < block.length; i++)
                {
                    rotations[i].value = MathHelper.LookAt(positions[i].value, cameraPosition, vec3.UnitY);
                    //positions[i].value += rotations[i].value * vec3.UnitZ * deltaTime;
                }
            }
Example #6
0
            public override void ProcessBlock(float deltaTime, BlockAccessor block)
            {
                var rot   = block.GetComponentData <Rotation>();
                var speed = block.GetReadOnlyComponentData <RotateComponent>();

                for (int i = 0; i < block.length; i++)
                {
                    var rotation = quat.FromAxisAngle(speed[i].rotationSpeed * deltaTime, vec3.UnitY);
                    rot[i].value = rot[i].value * rotation;
                }
            }
Example #7
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var positions = block.GetReadOnlyComponentData <Position>();
            var rotations = block.GetReadOnlyComponentData <Rotation>();
            var values    = block.GetComponentData <PhysicsBodyLockedValues>();

            for (int i = 0; i < block.length; i++)
            {
                values[i].pos = positions[i].value;
                values[i].rot = rotations[i].value;
            }
        }
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var position = block.GetReadOnlyComponentData <Position>();
            var oToW     = block.GetComponentData <ObjectToWorld>();

            for (int i = 0; i < block.length; i++)
            {
                vec3 pos = position[i].value;
                oToW[i].model  = mat4.Translate(pos);
                oToW[i].normal = mat3.Identity;
            }
        }
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var mesh       = block.GetSharedComponentData <MeshRenderer>();
            var boxes      = block.GetComponentData <BoundingBox>();
            var objToW     = block.GetReadOnlyComponentData <ObjectToWorld>();
            var initialBox = mesh.mesh.bounds;

            for (int i = 0; i < block.length; i++)
            {
                boxes[i].value = initialBox.TransformAabb(objToW[i].model);
            }
        }
Example #10
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var entities  = block.GetEntityData();
            var rbs       = block.GetComponentData <RigidBody>();
            var colliders = block.GetReadOnlyComponentData <InternalColliderHandle>();

            for (int i = 0; i < block.length; i++)
            {
                var shapeIdx = colliders[i].shapeIdx;


                int handle;
                if (rbs[i].mass == 0)
                {
                    afterUpdateCommands.AddComponent(entities[i],
                                                     new InternalRigidBodyHandle()
                    {
                        rigidBodyHandle = handle =
                            PhysicsSystem.Simulation.Bodies.Add(
                                BodyDescription.CreateKinematic(RigidPose.Identity,
                                                                new CollidableDescription(
                                                                    shapeIdx, 0.1f,
                                                                    new ContinuousDetectionSettings {
                            Mode = (ContinuousDetectionMode)rbs[i].detectionMode
                        }),
                                                                new BodyActivityDescription(sleepThreshold: 0.001f)))
                    });
                }
                else
                {
                    afterUpdateCommands.AddComponent(entities[i],
                                                     new InternalRigidBodyHandle()
                    {
                        rigidBodyHandle = handle =
                            PhysicsSystem.Simulation.Bodies.Add(
                                BodyDescription.CreateDynamic(RigidPose.Identity, colliders[i].inertia,
                                                              new CollidableDescription(
                                                                  shapeIdx, 0.1f,
                                                                  new ContinuousDetectionSettings {
                            Mode = (ContinuousDetectionMode)rbs[i].detectionMode
                        }),
                                                              new BodyActivityDescription(sleepThreshold: 0.001f)))
                    });
                }

                PhysicsSystem.rigidBodyEntityDictionary.Add(handle, entities[i]);
            }
        }
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var position = block.GetReadOnlyComponentData <Position>();
            var rotation = block.GetReadOnlyComponentData <Rotation>();
            var oToW     = block.GetComponentData <ObjectToWorld>();

            for (int i = 0; i < block.length; i++)
            {
                vec3 pos = position[i].value;
                quat rot = rotation[i].value;
                var  p   = mat4.Translate(pos);

                var matrix = p * rot.ToMat4;
                oToW[i].model  = matrix;
                oToW[i].normal = new mat3(oToW[i].model);
            }
        }
Example #12
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block, ECSWorld world)
        {
            var cameras   = block.GetReadOnlyComponentData <CameraFollow>();
            var positions = block.GetComponentData <Position>();

            for (int i = 0; i < block.length; i++)
            {
                var followTarget = cameras[i].entityToFollow;
                try
                {
                    if (world.EntityExists(followTarget))
                    {
                        var pos = world.ComponentManager.GetComponent <Position>(followTarget);
                        positions[i].value = pos.value - vec3.UnitZ * cameras[i].zDistance;
                    }
                }
                catch (ComponentNotFoundException) { }
            }
        }
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var position = block.GetReadOnlyComponentData <Position>();
            var scale    = block.GetReadOnlyComponentData <Scale>();
            var oToW     = block.GetComponentData <ObjectToWorld>();

            for (int i = 0; i < block.length; i++)
            {
                vec3 pos = position[i].value;
                vec3 scl = scale[i].value;

                var p = mat4.Translate(pos);
                var s = mat4.Scale(scl);

                var matrix = p * s;
                oToW[i].model = matrix;
                var inverse      = matrix.Inverse;
                var normalMatrix = new mat3(inverse.Transposed);
                oToW[i].normal = normalMatrix;
            }
        }
Example #14
0
        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]);
            }
        }
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var rbs  = block.GetComponentData <RigidBody>();
            var pos  = block.GetComponentData <Position>();
            var rot  = block.GetComponentData <Rotation>();
            var vel  = block.GetReadOnlyComponentData <Velocity>();
            var avel = block.GetReadOnlyComponentData <AngularVelocity>();

            if (Fma.IsSupported && Avx.IsSupported)
            {
                unsafe {
                    Vector128 <float> deltaF = Vector128.Create(deltaTime);
                    fixed(float *oldPosFloats = pos.Cast <Position, float>())
                    fixed(float *posFloats = pos.Cast <Position, float>())
                    fixed(float *velFloats = vel.Cast <Velocity, float>())
                    {
                        int i = 0;

                        for (; i < block.length; i += 4)
                        {
                            var op     = Sse.LoadAlignedVector128(&oldPosFloats[i]);
                            var p      = Sse.LoadAlignedVector128(&posFloats[i]);
                            var v      = Sse.LoadAlignedVector128(&velFloats[i]);
                            var result = Fma.MultiplyAdd(deltaF, v, p);
                            var bools  = Sse.CompareEqual(op, p);

                            Avx.MaskStore(&posFloats[i], bools, result);
                        }

                        for (i -= 4; i < block.length; i++)
                        {
                            if (oldPosFloats[i] == posFloats[i])
                            {
                                posFloats[i] = posFloats[i] + velFloats[i] * deltaTime;
                            }
                        }
                    }
                }

                for (int i = 0; i < block.length; i++)
                {
                    if (pos[i].value == rbs[i].lastPosition && rot[i].value == rbs[i].lastRotation)
                    {
                        quat x = quat.FromAxisAngle(avel[i].value.x * deltaTime, vec3.UnitX);
                        quat y = quat.FromAxisAngle(avel[i].value.y * deltaTime, vec3.UnitY);
                        quat z = quat.FromAxisAngle(avel[i].value.z * deltaTime, vec3.UnitZ);
                        rot[i].value = rot[i].value * x * y * z;

                        rbs[i].lastPosition = pos[i].value;
                        rbs[i].lastRotation = rot[i].value;
                    }
                }
            }
            else
            {
                for (int i = 0; i < block.length; i++)
                {
                    if (pos[i].value == rbs[i].lastPosition && rot[i].value == rbs[i].lastRotation)
                    {
                        pos[i].value += vel[i].value * deltaTime;

                        quat x = quat.FromAxisAngle(avel[i].value.x * deltaTime, vec3.UnitX);
                        quat y = quat.FromAxisAngle(avel[i].value.y * deltaTime, vec3.UnitY);
                        quat z = quat.FromAxisAngle(avel[i].value.z * deltaTime, vec3.UnitZ);
                        rot[i].value = rot[i].value * x * y * z;

                        rbs[i].lastPosition = pos[i].value;
                        rbs[i].lastRotation = rot[i].value;
                    }
                }
            }
        }
Example #16
0
        public override void ProcessBlock(float deltaTime, BlockAccessor block)
        {
            var positions     = block.GetComponentData <Position>();
            var rotations     = block.GetComponentData <Rotation>();
            var cameraFlights = block.GetReadOnlyComponentData <CameraFlightComponent>();


            for (int i = 0; i < block.length; i++)
            {
                float flightSpeed = cameraFlights[i].flightSpeed;

                if (Input.GetKey(Key.Space))
                {
                    vec2  mouseDelta = Input.GetMouseDelta() * 0.002f;
                    dvec3 euler      = rotations[i].value.EulerAngles;
                    euler.x += mouseDelta.y;
                    euler.y += mouseDelta.x;


                    //rotations[i].value = new quat(new vec3((float) euler.x, (float) euler.y, (float) euler.z));

                    quat inverse = rotations[i].value.Inverse;
                    quat x       = quat.FromAxisAngle(-mouseDelta.x, inverse * vec3.UnitY);
                    quat y       = quat.FromAxisAngle(mouseDelta.y, vec3.UnitX);
                    rotations[i].value = rotations[i].value * x * y;
                }

                //if (Input.GetKey(Key.W)) {
                //	var dir = Vector3.Transform(Vector3.UnitZ, rotations[i].value);
                //	positions[i].value += dir * deltaTime * flightSpeed;
                //}
                //if (Input.GetKey(Key.S)) {
                //	var dir = Vector3.Transform(-Vector3.UnitZ, rotations[i].value);
                //	positions[i].value += dir * deltaTime * flightSpeed;
                //}
                //if (Input.GetKey(Key.A)) {
                //	var dir = Vector3.Transform(Vector3.UnitX, rotations[i].value);
                //	positions[i].value += dir * deltaTime * flightSpeed;
                //}
                //if (Input.GetKey(Key.D)) {
                //	var dir = Vector3.Transform(-Vector3.UnitX, rotations[i].value);
                //	positions[i].value += dir * deltaTime * flightSpeed;
                //}

                if (Input.GetKey(Key.W))
                {
                    var dir = rotations[i].value * vec3.UnitZ;
                    positions[i].value += dir * deltaTime * flightSpeed;
                }
                if (Input.GetKey(Key.S))
                {
                    var dir = rotations[i].value * -vec3.UnitZ;
                    positions[i].value += dir * deltaTime * flightSpeed;
                }
                if (Input.GetKey(Key.A))
                {
                    var dir = rotations[i].value * vec3.UnitX;
                    positions[i].value += dir * deltaTime * flightSpeed;
                }
                if (Input.GetKey(Key.D))
                {
                    var dir = rotations[i].value * -vec3.UnitX;
                    positions[i].value += dir * deltaTime * flightSpeed;
                }
            }
        }