A class for working with Vectors
Inheritance: System.Object
Exemplo n.º 1
0
        void rotateElbowWithHandFoward()
        {
            var     s            = handSettings;
            Vector3 handRightVec = target.rotation * armDirection;

            float elbowTargetAngleForward = VectorHelpers.getAngleBetween(lowerArmRotation * armDirection, handRightVec,
                                                                          lowerArmRotation * Vector3.up, lowerArmRotation * Vector3.forward);

            float deltaElbowForward = (elbowTargetAngleForward + (left ? -s.handDeltaForwardOffset : s.handDeltaForwardOffset)) / 180f;

            if (Mathf.Abs(deltaElbowForward) < s.handDeltaForwardDeadzone)
            {
                deltaElbowForward = 0f;
            }
            else
            {
                deltaElbowForward = (deltaElbowForward - Mathf.Sign(deltaElbowForward) * s.handDeltaForwardDeadzone) / (1f - s.handDeltaForwardDeadzone);
            }

            deltaElbowForward             = Mathf.Sign(deltaElbowForward) * Mathf.Pow(Mathf.Abs(deltaElbowForward), s.handDeltaForwardPow) * 180f;
            interpolatedDeltaElbowForward = Mathf.LerpAngle(interpolatedDeltaElbowForward, deltaElbowForward, Time.deltaTime / s.rotateElbowWithHandDelay);

            float signedInterpolated = interpolatedDeltaElbowForward.toSignedEulerAngle();

            rotateElbow(signedInterpolated * s.handDeltaForwardFactor);
        }
Exemplo n.º 2
0
    /*
     * Match this part to another, optionally forcing cardinal axis alignment
     * */
    public void MatchTo(BodyPart other, bool isCardinalAlignment)
    {
        // match the parts to each other
        oppositePart       = other;
        other.oppositePart = this;

        // error if the bones are on top of each other
        if ((bone.position - other.bone.position).sqrMagnitude == 0f)
        {
            Debug.LogWarning(string.Format("Could not compute corresponding axes for BodyParts {0} and {1} because they are coincident.", name, other.name));
        }

        // compute match axes for each part
        Vector3 reflectionDirection = (bone.position - other.bone.position).normalized;

        other.oppositeForward = other.bone.InverseTransformDirection(VectorHelpers.MirrorPointAcrossPlane(bone.forward, reflectionDirection)).normalized;
        other.oppositeRight   = other.bone.InverseTransformDirection(VectorHelpers.MirrorPointAcrossPlane(bone.right, reflectionDirection)).normalized;
        other.oppositeUp      = other.bone.InverseTransformDirection(VectorHelpers.MirrorPointAcrossPlane(bone.up, reflectionDirection)).normalized;
        oppositeForward       = bone.InverseTransformDirection(VectorHelpers.MirrorPointAcrossPlane(other.bone.forward, reflectionDirection)).normalized;
        oppositeRight         = bone.InverseTransformDirection(VectorHelpers.MirrorPointAcrossPlane(other.bone.right, reflectionDirection)).normalized;
        oppositeUp            = bone.InverseTransformDirection(VectorHelpers.MirrorPointAcrossPlane(other.bone.up, reflectionDirection)).normalized;

        // if isCardinalAlignment, then map the nearest cardinal axis as opposed to the raw axis
        if (isCardinalAlignment)
        {
            other.oppositeForward = VectorHelpers.FindNearestCardinalAxis(other.oppositeForward);
            other.oppositeRight   = VectorHelpers.FindNearestCardinalAxis(other.oppositeRight);
            other.oppositeUp      = VectorHelpers.FindNearestCardinalAxis(other.oppositeUp);
            oppositeForward       = VectorHelpers.FindNearestCardinalAxis(oppositeForward);
            oppositeRight         = VectorHelpers.FindNearestCardinalAxis(oppositeRight);
            oppositeUp            = VectorHelpers.FindNearestCardinalAxis(oppositeUp);
        }
    }
Exemplo n.º 3
0
        public override void Update()
        {
            if (World.Ticks >= CreatedTicks + TicksToBuild)
            {
                Constructing = false;
            }

            if (!Constructing)
            {
                if (TargetEntity != null)
                {
                    float distance = Vector2.Distance(new Vector2(Position.X, Position.Y), new Vector2(TargetEntity.Position.X, TargetEntity.Position.Y));
                    if (distance > Range || !TargetEntity.IsAlive)
                    {
                        TargetEntity = null;
                    }
                }
                if (TargetEntity == null)
                {
                    IEntity nearestEnemyHarvester = World.Entities.Where(e => e.Player != Player && e is Harvester)
                                                    .OrderBy(e => Vector2.Distance(new Vector2(Position.X, Position.Y), new Vector2(e.Position.X, e.Position.Y)))
                                                    .FirstOrDefault();
                    if (nearestEnemyHarvester != null)
                    {
                        float distance = Vector2.Distance(new Vector2(Position.X, Position.Y), new Vector2(nearestEnemyHarvester.Position.X, nearestEnemyHarvester.Position.Y));
                        if (distance < Range)
                        {
                            TargetEntity = nearestEnemyHarvester;
                        }
                    }
                }

                if (TargetEntity != null)
                {
                    float targetRotation   = VectorHelpers.GetRotationToFace(PositionVector, TargetEntity.PositionVector) ?? Rotation;
                    float rotationDiff     = targetRotation - Rotation;
                    float rotationPerFrame = RotationSpeed;
                    //Console.WriteLine("Source: {0}, Target: {1}, Diff: {2} MaxPerFrame: {3}", Rotation, targetRotation, rotationDiff, rotationPerFrame);
                    if (rotationDiff < rotationPerFrame)
                    {
                        Rotation = targetRotation;
                        if (LastShotTicks == null || World.Ticks - LastShotTicks >= FiringIntervalTicks)
                        {
                            int x = Math.Max(0, Position.X - Sprites.Turret.Width / 2);
                            int y = Math.Max(0, Position.Y - Sprites.Turret.Height / 2);


                            Projectile bullet = new CannonShot(World, Player, Position, Target);
                            World.AddProjectile(bullet);
                            LastShotTicks = World.Ticks;
                            Sounds.CannonShot.Play();
                        }
                    }
                    else
                    {
                        Rotation += rotationPerFrame;
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void rotateHand()
        {
            if (handSettings.useWristRotation)
            {
                Vector3 handUpVec    = target.rotation * Vector3.up;
                float   forwardAngle = VectorHelpers.getAngleBetween(lowerArmRotation * Vector3.right, target.rotation * Vector3.right,
                                                                     lowerArmRotation * Vector3.up, lowerArmRotation * Vector3.forward);

                // todo reduce influence if hand local forward rotation is high (hand tilted inside)
                Quaternion handForwardRotation = Quaternion.AngleAxis(-forwardAngle, lowerArmRotation * Vector3.forward);
                handUpVec = handForwardRotation * handUpVec;

                float elbowTargetAngle = VectorHelpers.getAngleBetween(lowerArmRotation * Vector3.up, handUpVec,
                                                                       lowerArmRotation * Vector3.forward, lowerArmRotation * armDirection);

                elbowTargetAngle = Mathf.Clamp(elbowTargetAngle, -90f, 90f);
                if (arm.wrist1 != null)
                {
                    setWrist1Rotation(Quaternion.AngleAxis(elbowTargetAngle * .3f, lowerArmRotation * armDirection) * lowerArmRotation);
                }
                if (arm.wrist2 != null)
                {
                    setWrist2Rotation(Quaternion.AngleAxis(elbowTargetAngle * .8f, lowerArmRotation * armDirection) * lowerArmRotation);
                }
            }
            setHandRotation(target.rotation);
        }
Exemplo n.º 5
0
        public void MoveCamera(double forward, double side)
        {
            var cameraForward = (_activeCamera.LookAt - _activeCamera.Position).Normalize(2);
            var cameraSide    = VectorHelpers.CrossProduct3D(cameraForward, _activeCamera.UpVector);

            _activeCamera.Position = _activeCamera.Position + cameraForward * forward + cameraSide * side;
        }
Exemplo n.º 6
0
        public override bool Scatter(Ray r, HitRecord hitRecord, out Vector3 attenuation, out Ray scattered)
        {
            Vector3 reflected = VectorHelpers.Reflect(r.Direction.GetUnitVector(), hitRecord.SurfaceNormal);

            scattered   = new Ray(hitRecord.HitPoint, reflected + Fuzz * VectorHelpers.GetRandomInUnitSphere());
            attenuation = Albedo;
            return(Vector3.Dot(scattered.Direction, hitRecord.SurfaceNormal) > 0);
        }
Exemplo n.º 7
0
        public override bool Scatter(Ray r, HitRecord hitRecord, out Vector3 attenuation, out Ray scattered)
        {
            Vector3 target = hitRecord.HitPoint + hitRecord.SurfaceNormal + VectorHelpers.GetRandomInUnitSphere();

            scattered   = new Ray(hitRecord.HitPoint, target - hitRecord.HitPoint);
            attenuation = Texture.GetValue(0, 0, hitRecord.HitPoint);
            return(true);
        }
Exemplo n.º 8
0
        public override void Update()
        {
            A10   a10         = World.GetEntities <A10>().First();
            float a10Distance = Vector2.Distance(a10.PositionVector, PositionVector);

            if (State == SamState.Closed)
            {
                if (a10Distance < Range)
                {
                    State             = SamState.Opening;
                    StateChangedTicks = World.Ticks;
                }
            }
            else if (State == SamState.Opening)
            {
                if (World.Ticks - StateChangedTicks >= TicksToOpen)
                {
                    State             = SamState.Open;
                    StateChangedTicks = World.Ticks;
                }
            }
            else if (State == SamState.Open)
            {
                if (a10Distance < Range)
                {
                    Rotation = VectorHelpers.GetRotationToFace(PositionVector, a10.PositionVector) ?? Rotation;

                    if (World.Ticks - LastShotTicks > TicksBetweenShots)
                    {
                        World.AddProjectile(new Patriot(World, Player, Position, a10));
                        World.AddExplosion(new SamMuzzle(World, this, Rotation));
                        Sounds.Rocket1.Play();
                        LastShotTicks = World.Ticks;
                    }
                }
                else
                {
                    State             = SamState.Closing;
                    StateChangedTicks = World.Ticks;
                }
            }
            else if (State == SamState.Closing)
            {
                if (World.Ticks - StateChangedTicks >= TicksToClose)
                {
                    State             = SamState.Closed;
                    StateChangedTicks = World.Ticks;
                }
            }
            else
            {
                throw new Exception("what");
            }


            base.Update();
        }
Exemplo n.º 9
0
        public void FindRotationAdjustmentTest()
        {
            Assert.AreEqual(3 * MathHelper.PiOver4, VectorHelpers.FindRotationAdjustment(new Vector2(1, 0), new Vector2(0, 1), MathHelper.ToRadians(0)), 0.01);
            Assert.AreEqual(1 * MathHelper.PiOver4, VectorHelpers.FindRotationAdjustment(new Vector2(1, 0), new Vector2(0, 1), MathHelper.ToRadians(90)), 0.01);
            Assert.AreEqual(-1 * MathHelper.PiOver4, VectorHelpers.FindRotationAdjustment(new Vector2(1, 0), new Vector2(0, 1), MathHelper.ToRadians(180)), 0.01);
            Assert.AreEqual(-3 * MathHelper.PiOver4, VectorHelpers.FindRotationAdjustment(new Vector2(1, 0), new Vector2(0, 1), MathHelper.ToRadians(270)), 0.01);

            Assert.AreEqual(-1 * MathHelper.PiOver4, VectorHelpers.FindRotationAdjustment(new Vector2(0, 1), new Vector2(1, 0), MathHelper.ToRadians(0)), 0.01);
        }
Exemplo n.º 10
0
 private static Vector3[] PerlinGenerate()
 {
     Vector3[] res = new Vector3[256];
     for (int i = 0; i < 256; i++)
     {
         res[i] = new Vector3(2 * VectorHelpers.RandomFloat() - 1, 2 * VectorHelpers.RandomFloat() - 1, 2 * VectorHelpers.RandomFloat() - 1).GetUnitVector();
     }
     return(res);
 }
Exemplo n.º 11
0
        List <LineD> BuildLineGrid(int HalfHeight, int HalfWidth)
        {
            Vector3D CenterStart = MyKernel.BeamDrawer.BeamStart;
            Vector3D CenterEnd   = MyKernel.BeamDrawer.BeamEnd;

            Vector3D UpOffset    = Vector3D.Normalize(MyKernel.Block.WorldMatrix.Up) * 0.5;
            Vector3D RightOffset = Vector3D.Normalize(MyKernel.Tool.WorldMatrix.Right) * 0.5;

            return(VectorHelpers.BuildLineGrid(HalfHeight, HalfWidth, CenterStart, CenterEnd, UpOffset, RightOffset));
        }
Exemplo n.º 12
0
        public Camera()
        {
            FieldOfView = Math.PI / 4;
            AspectRatio = 1;
            NearPlane   = 1;
            FarPlane    = 100;

            UpVector = VectorHelpers.Create(0, 1, 0);
            Position = VectorHelpers.Create(0, 0, 0);
            LookAt   = VectorHelpers.Create(0, 0, -1);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Returns the regular old width, height, etc. because static images do not rotate. However, leaving this version of the function out would require some hacky coding over in the SensorField.Update() function.
 /// </summary>
 /// <param name="includeRotatedPixels"></param>
 /// <returns></returns>
 public override RotationPacket getRotationPacketWithoutSensors(bool includeRotatedPixels = false)
 {
     if (includeRotatedPixels)
     {
         return(new RotationPacket(Texture.Width, Texture.Height, VectorHelpers.asXNAPoint(Position), VectorHelpers.asXNAPoint(Position + new Vector2(Texture.Width, Texture.Height)), TextureAsColorArray));
     }
     else
     {
         return(new RotationPacket(Texture.Width, Texture.Height, VectorHelpers.asXNAPoint(Position), VectorHelpers.asXNAPoint(Position + new Vector2(Texture.Width, Texture.Height))));
     }
 }
Exemplo n.º 14
0
 public void GetRotationToFaceTest()
 {
     Assert.AreEqual(null, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(0, 0)));
     Assert.AreEqual(MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(1, 1)).Value, 0.01);
     Assert.AreEqual(MathHelper.PiOver2, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(0, 1)).Value, 0.01);
     Assert.AreEqual(3 * MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(-1, 1)).Value, 0.01);
     Assert.AreEqual(MathHelper.Pi, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(-1, 0)).Value, 0.01);
     Assert.AreEqual(-3 * MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(-1, -1)).Value, 0.01);
     Assert.AreEqual(-1 * MathHelper.PiOver2, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(0, -1)).Value, 0.01);
     Assert.AreEqual(-1 * MathHelper.PiOver4, VectorHelpers.GetRotationToFace(Vector2.Zero, new Vector2(1, -1)).Value, 0.01);
 }
Exemplo n.º 15
0
 public void GetVectorInDirectionTest()
 {
     Assert.AreEqual(new Vector2(1, 0), VectorHelpers.GetVectorInDirection(0));
     Assert.AreEqual(new Vector2(1, 1), VectorHelpers.GetVectorInDirection(MathHelper.PiOver4));
     Assert.AreEqual(new Vector2(0, 1), VectorHelpers.GetVectorInDirection(MathHelper.PiOver2));
     Assert.AreEqual(new Vector2(-1, 1), VectorHelpers.GetVectorInDirection(3 * MathHelper.PiOver4));
     Assert.AreEqual(new Vector2(-1, 0), VectorHelpers.GetVectorInDirection(MathHelper.Pi));
     Assert.AreEqual(new Vector2(-1, -1), VectorHelpers.GetVectorInDirection(-3 * MathHelper.PiOver4));
     Assert.AreEqual(new Vector2(0, -1), VectorHelpers.GetVectorInDirection(-1 * MathHelper.PiOver2));
     Assert.AreEqual(new Vector2(1, -1), VectorHelpers.GetVectorInDirection(-1 * MathHelper.PiOver4));
 }
Exemplo n.º 16
0
 public Projectile(World world, Player player, Point position, Point target, IEntity targetEntity)
 {
     World          = world;
     Player         = player;
     PositionVector = new Vector2(position.X, position.Y);
     Target         = target;
     IsAlive        = true;
     CreatedTicks   = world.Ticks;
     TargetEntity   = targetEntity;
     Rotation       = VectorHelpers.GetRotationToFace(PositionVector, new Vector2(target.X, target.Y)) ?? 0;
 }
Exemplo n.º 17
0
 public override void Update()
 {
     if (Position != Target)
     {
         float targetRotation = VectorHelpers.GetRotationToFace(PositionVector, new Vector2(Target.X, Target.Y)) ?? Rotation;
         if (Rotation == targetRotation)
         {
             PositionVector = VectorHelpers.MoveInDirection(PositionVector, targetRotation, MovementSpeed);
         }
         Rotation = targetRotation;
     }
 }
Exemplo n.º 18
0
    internal void Tick()
    {
        mUnitComponent.Acceleration = Vector2.ClampMagnitude(mUnitComponent.Acceleration, mUnitComponent.MaxAcceleration);

        mUnitComponent.Velocity += (mUnitComponent.Acceleration * Time.deltaTime);
        mUnitComponent.Velocity  = Vector2.ClampMagnitude(mUnitComponent.Velocity, mUnitComponent.MaxVelocity);

        if (mUnitComponent.Velocity.sqrMagnitude > kEpsilon)
        {
            mUnitComponent.Position += mUnitComponent.Velocity * Time.deltaTime;
            transform.position       = VectorHelpers.Vector2ToVector3(mUnitComponent.Position, transform.position.y);
        }
    }
Exemplo n.º 19
0
        public override void Draw(SpriteBatch spriteBatch)
        {
            int x = Position.X - Sprites.A10.Width / 2;
            int y = Position.Y - Sprites.A10.Height / 2;

            spriteBatch.Draw(Sprites.Shadow.Texture, new Rectangle(x, y + FlyingHeight, Sprites.Shadow.Width, Sprites.Shadow.Height), Color.White);

            if (TsGame.DebugEnabled)
            {
                SpriteHelper.DrawLine(spriteBatch, PositionVector, VectorHelpers.MoveInDirection(PositionVector, Rotation, 50), Color.White, ZOrder.Debug);
            }

            base.Draw(spriteBatch);
        }
Exemplo n.º 20
0
    private void RotationLogic()
    {
        float rotationAngle = vehicle.inputLeftStick.x;

        if (inDanger)
        {
            VehicleController otherShip = imminentDanger.transform.GetComponent <VehicleController>();

            if (otherShip)
            {
                Vector2 otherShipTrajectory = otherShip.velocity * dangerRayTimeFrame;
                float   trajectoryAngle     = -VectorHelpers.AngleSigned(otherShip.transform.position - vehicle.transform.position, (Vector2)otherShip.transform.position + otherShipTrajectory - (Vector2)vehicle.transform.position, transform.up);
                rotationAngle = trajectoryAngle + (trajectoryAngle > 0 ? 5 : -5);
                //Debug.Log("Trajectory Angle: " + trajectoryAngle);
            }
            else
            {
                float percentVelocityInDanger = Vector2.Dot(vehicle.velocity.normalized, imminentDanger.normal);
                percentVelocityInDanger = Mathf.Clamp(percentVelocityInDanger, 0.6f, 1);

                Vector2 reflection = Vector2.Reflect((imminentDanger.point - (Vector2)transform.position), imminentDanger.normal).normalized;
                dangerAvoidanceVector = Vector2.Lerp(reflection, imminentDanger.normal, percentVelocityInDanger);

                rotationAngle = VectorHelpers.AngleSigned(transform.forward, dangerAvoidanceVector, transform.up);
            }
        }
        else
        {
            rotationAngle = VectorHelpers.AngleSigned(transform.forward, (destination - transform.position).normalized, transform.up);
        }

        if ((inDanger && Mathf.Abs(rotationAngle) <= minDangerAngle) || (!inDanger && Mathf.Abs(rotationAngle) <= minTargetAngle))
        {
            rotationAngle = 0;
        }

        //float maxRotationAngle = 360 * vehicle.revolutionsPerSecond * Time.fixedDeltaTime;
        float maxRotationAngle = 360 * vehicle.cappedRotSpeed * Time.fixedDeltaTime;
        float steerPercent     = Mathf.Clamp(rotationAngle / maxRotationAngle, -1, 1);

        if (steerPercent != 0)
        {
            vehicle.inputLeftStick = new Vector2(Mathf.Lerp(vehicle.inputLeftStick.x, steerPercent, Time.deltaTime * steerLerp), 0); steering = true;
        }
        else if (steering)
        {
            vehicle.inputLeftStick = Vector2.zero; steering = false;
        }
    }
Exemplo n.º 21
0
    void OnParticleCollision(GameObject other)
    {
        if (other != transform.root.gameObject)
        {
            //Debug.Log(other.name);
            Rigidbody2D otherBody = other.GetComponent <Rigidbody2D>();
            if (otherBody)
            {
                mainThruster.GetCollisionEvents(other, collisionEvents);
                for (int i = 0; i < collisionEvents.Count; i++)
                {
                    Vector3 incidentVelocity = collisionEvents[i].velocity;
                    Vector3 intersection     = collisionEvents[i].intersection;
                    Vector3 intersectionDir  = intersection - transform.position;
                    if (otherBody != vehicleController.vehicleBody && Vector3.Dot(transform.forward, intersectionDir.normalized) > 0.9f)
                    {
                        //Vector3 colliderSize = otherBody.GetComponentInChildren<Collider2D>().bounds.size;
                        //float largestSide = Mathf.Max(colliderSize.x, colliderSize.y, colliderSize.z);

                        //float totalParticles = mainThruster.particleCount + secondaryThruster.particleCount;
                        //float percentVelocity = incidentVelocity.magnitude / (mainThruster.startSpeed + secondaryThruster.startSpeed) / 2f;
                        //if (percentVelocity < 0) percentVelocity = 0;
                        //if (percentVelocity > 1) percentVelocity = 1;

                        float incidentDistance = intersectionDir.magnitude, flameLength = mainThruster.startSpeed * mainThruster.startLifetime;
                        //float torqueSign =
                        //float torqueSign = (Vector3.Dot(otherBody.transform.right, intersectionDir.normalized) * -1) * (Vector3.Dot(otherBody.transform.forward, intersectionDir.normalized));
                        float torqueSign = Mathf.Sin(2 * VectorHelpers.AngleSigned(otherBody.transform.forward, intersectionDir.normalized, Vector3.forward)) > 0 ? -1 : 1;
                        //float percentTorque = Mathf.Clamp01(incidentDistance / largestSide);
                        float percentForce = 1 - (incidentDistance / flameLength);
                        if (percentForce < 0)
                        {
                            percentForce = 0;
                        }
                        if (percentForce > 1)
                        {
                            percentForce = 1;
                        }

                        float   forceOnPoint = (vehicleController.thrustForce * percentForce / vehicleController.thrusters.Length) / Mathf.Clamp(mainThruster.particleCount / 5, 1, 1000);
                        Vector3 thrustForce  = incidentVelocity.normalized * forceOnPoint;
                        otherBody.AddForce(thrustForce);
                        otherBody.AddTorque(forceOnPoint * incidentDistance * torqueSign);
                        Debug.DrawRay(intersection, thrustForce, Color.black);
                    }
                }
            }
        }
    }
Exemplo n.º 22
0
    /*
     * Draw a ball at a part's location
     * */
    void DrawBall(BodyPart part, float scaleFactor)
    {
        if (part == null)
        {
            return;
        }

        Handles.SphereCap(0, part.bone.position, part.bone.rotation, _ballSize * VectorHelpers.MaxValue(part.bone.root.localScale));

        // draw a line to the parent part if it is defined
        Handles.color = linesColor;
        if (part.parentPart != null)
        {
            Handles.DrawLine(part.bone.position, part.parentPart.bone.position);
        }
    }
Exemplo n.º 23
0
 private static int[] PerlinGeneratePerm()
 {
     int[] res = new int[256];
     for (int i = 0; i < 256; i++)
     {
         res[i] = i;
     }
     // Permute the array of 0..255
     for (int i = 0; i < 256; i++)
     {
         int swithedIndex = (int)((i + 1) * VectorHelpers.RandomFloat());
         int temp         = res[i];
         res[i]            = res[swithedIndex];
         res[swithedIndex] = temp;
     }
     return(res);
 }
Exemplo n.º 24
0
        public void TestTransformComputation()
        {
            EuclideanTransform t;
            ErrorElements      errorElements;

            ConstructTestCase(out t, out errorElements);

            var minimizer = new PointToPlaneErrorMinimizer();
            var solvedT   = KnownCorrespondenceErrorMinimizer.IterativeSolveForTransform(errorElements, minimizer);

            float dist = (t.translation - solvedT.translation).Length();

            Assert.IsTrue(Precision.AlmostEqualInDecimalPlaces(0.0f, dist, 3));

            float angle = VectorHelpers.AngularDistance(t.rotation, solvedT.rotation);

            Assert.IsTrue(Precision.AlmostEqualInDecimalPlaces(0.0, Math.IEEERemainder(angle, Math.PI * 2), 3));
        }
Exemplo n.º 25
0
        protected virtual void rotateShoulderRight()
        {
            float heightDiff         = vrTrackingReferences.hmd.transform.position.y;
            float relativeHeightDiff = heightDiff / PoseManager.Instance.playerHeightHmd;

            float headRightRotation = VectorHelpers.getAngleBetween(shoulder.transform.forward,
                                                                    avatarTrackingReferences.hmd.transform.forward,
                                                                    Vector3.up, shoulder.transform.right) + rightRotationHeadRotationOffset;
            float heightFactor = Mathf.Clamp(relativeHeightDiff - rightRotationStartHeight, 0f, 1f);

            shoulderRightRotation  = heightFactor * rightRotationHeightFactor;
            shoulderRightRotation += Mathf.Clamp(headRightRotation * rightRotationHeadRotationFactor * heightFactor, 0f, 50f);

            Quaternion deltaRot = Quaternion.AngleAxis(shoulderRightRotation, shoulder.transform.right);


            shoulder.transform.rotation = deltaRot * shoulder.transform.rotation;
            positionShoulderRelative();
        }
Exemplo n.º 26
0
        private void DrawDebugRayGrid()
        {
            int RayGridSize = ToolCharacteristics.WelderGrinderWorkingZoneWidth;

            Vector4  _internalBeamColor = InternalBeamColor;
            Vector3D CenterStart        = MyKernel.BeamDrawer.BeamStart;
            Vector3D CenterEnd          = MyKernel.BeamDrawer.BeamEnd;

            Vector3D UpOffset    = Vector3D.Normalize(MyKernel.Block.WorldMatrix.Up) * 0.5;
            Vector3D RightOffset = Vector3D.Normalize(MyKernel.Tool.WorldMatrix.Right) * 0.5;

            var VirtualLineGrid = VectorHelpers.BuildLineGrid(RayGridSize, RayGridSize, CenterStart, CenterEnd, UpOffset, RightOffset);

            VirtualLineGrid.Remove(new LineD(BeamStart, BeamEnd));
            foreach (LineD VirtualLine in VirtualLineGrid)
            {
                MySimpleObjectDraw.DrawLine(VirtualLine.From, VirtualLine.To, MyStringId.GetOrCompute("WeaponLaser"), ref _internalBeamColor, 0.1f);
            }
        }
Exemplo n.º 27
0
    private void CalculateTrajectory()
    {
        if (projectileBody && cappedRocketRotSpeed > 0)
        {
            thrustPercent = 1;

            if (lockTarget)
            {
                Vector3 displacement = lockTarget.position - transform.position;
                if (displacement.magnitude <= lockRange)
                {
                    float angle            = VectorHelpers.AngleSigned(transform.forward, displacement.normalized, Vector3.forward);
                    float maxRotationAngle = 360 * cappedRocketRotSpeed * Time.fixedDeltaTime;
                    percentRotation = Mathf.Clamp(angle / maxRotationAngle, -1, 1);
                    //projectileBody.angularDrag = torqueForce / ((cappedRocketRotSpeed * VehicleController.KI2ME * projectileBody.mass) + (Time.fixedDeltaTime / torqueForce));
                }
            }
        }
    }
Exemplo n.º 28
0
    public void Initialize()
    {
        GroupComponent groupComponent = transform.parent.GetComponent <GroupComponent>();

        UseDestination = groupComponent.UseDestination;
        if (UseDestination)
        {
            Destination = VectorHelpers.Vector3ToVector2(groupComponent.DestinationWaypoint.position);
        }
        else
        {
            Direction = groupComponent.Direction;
        }
        Position = VectorHelpers.Vector3ToVector2(transform.position);
        Vector3 localScale = transform.localScale;
        float   scale      = ((localScale.x + localScale.y + localScale.z) / 3);

        Radius = (scale / 2);
    }
Exemplo n.º 29
0
    /*
     * Convert this part's shapeSize into the opposite part's space
     * */
    public void PasteShapeSizeToOpposite(bool applyScale)
    {
        Vector3 s = TransformPointToOpposite(shapeSize, applyScale);

        if (s.sqrMagnitude == 0f)
        {
            return;                               // early out if there is invalid scale
        }
        oppositePart.shapeSize = new Vector3(Mathf.Abs(s.x), Mathf.Abs(s.y), Mathf.Abs(s.z));
        if (oppositePart.shapeType == ShapeType.Sphere)
        {
            oppositePart.shapeSize = Vector3.one * VectorHelpers.MaxValue(oppositePart.shapeSize);
        }
        else if (oppositePart.shapeType == ShapeType.Capsule)
        {
            CapsuleDirection currentDirection = oppositePart.capsuleDirection;
            oppositePart.capsuleDirection = capsuleDirection;
            oppositePart.FlipCapsule(currentDirection);
        }
    }
Exemplo n.º 30
0
        public override void Update()
        {
            Point   target = TargetEntity != null ? TargetEntity.Position : Target;
            Vector2 targetPositionVector = new Vector2(target.X, target.Y);

            float rotationDiff = VectorHelpers.FindRotationAdjustment(PositionVector, targetPositionVector, Rotation);
            float turnFactor   = Math.Min(TurnRate / Math.Abs(rotationDiff), 1);
            float amountToTurn = rotationDiff * turnFactor;

            Rotation += amountToTurn;

            float distanceToTarget = Vector2.Distance(targetPositionVector, PositionVector);
            float distanceToFly    = Math.Min(distanceToTarget, MovementSpeed);

            PositionVector = VectorHelpers.MoveInDirection(PositionVector, Rotation, distanceToFly);

            if (Vector2.Distance(targetPositionVector, PositionVector) < ExplosionRadius)
            {
                Explode();
            }
        }