public MyPhysics Physics; //Physics Component //Used to Initialise Bullet public void Init(BulletInit Initiate) { //Get Components Physics = GetComponent <MyPhysics>(); //Gets Physics component from object (Makes sure physics is up to date) Transformation = GetComponent <myTransformation>(); //Gets Transformation Component from object (Makes sure transformation is up to date) //Reset Timeout timeoutDestructor = 0; //Set Scale Transformation.Scale = new MyVector3(2, 2, 2); //Set Physical Mass Physics.Mass = Initiate.mass; //Set Rotation And Translation Transformation.Translation = Initiate.GunPosition; //Apply a force in the direction of the gun and times it by the firing speed Physics.Force += VectorMaths.EulerAnglesToDirection(new MyVector3(Initiate.GunRotation.x, -Initiate.GunRotation.y, 0)) * Initiate.FireSpeed; Physics.Bouncy = true; //Force Bullet to be alive Alive = true; }
//used to zoom the camera into the sun to show off the AABB on the space ship //uses the custom LERP function to bring the camera between the start position and the sun void Update() { GameObject controller = GameObject.Find("SpeedController"); SpeedControl spController = controller.GetComponent <SpeedControl>(); speedScalar = spController.Speed; if (Input.GetKeyDown(KeyCode.Keypad1)) { PlanetSelect = GameObject.Find("Sun"); IsFollowing = true; returntobase = false; } if (Input.GetKeyDown(KeyCode.Keypad0)) { returntobase = true; IsFollowing = false; } if (IsFollowing == true & returntobase == false) { Vector3 temp = PlanetSelect.transform.position; Vector3 newPos = new Vector3(temp.x, temp.y + 30, temp.z - 50); transform.position = VectorMaths.LERP(transform.position, newPos, Time.deltaTime); } if (IsFollowing == false & returntobase == true) { Vector3 temp1 = new Vector3(0, 200, -400); transform.position = VectorMaths.LERP(transform.position, temp1, Time.deltaTime); } }
public override void OnInspectorGUI() { VectorMaths script = (VectorMaths)target; Vector3 calculatedVector = script.CalculatedVec, StartPoint = Vector3.zero; script.UsingTransform = GUILayout.Toggle(script.UsingTransform, "Use Transforms"); if (script.UsingTransform) { script.PointA_Trans = (Transform)EditorGUILayout.ObjectField("Point A", script.PointA_Trans, typeof(Transform), true); script.PointB_Trans = (Transform)EditorGUILayout.ObjectField("Point B", script.PointB_Trans, typeof(Transform), true); if (!Application.isPlaying) { script.CalculateVector(); } } else { script.PointA_Vec = EditorGUILayout.Vector3Field("Point A", script.PointA_Vec); script.PointB_Vec = EditorGUILayout.Vector3Field("Point B", script.PointB_Vec); if (!Application.isPlaying) { script.CalculateVector(); } } GUILayout.Label("Vector is : " + calculatedVector); GUILayout.Label("Vector Length is: " + calculatedVector.magnitude); }
public void Rotate(float degrees) { for (int i = 0; i < lander.Length; i++) { lander[i] = VectorMaths.Pivot(new Vector2f(), degrees, VectorMaths.GetDistance(center, lander[i]), i); } }
public static bool Collide(BoundingCapsule capsule, BoundingCircle otherCircle) { float CombinedRadius = capsule.Radius + otherCircle.Radius; float DistanceFromFloat = Mathf.Sqrt(VectorMaths.SqDistanceFromFloat(capsule.A, capsule.B, otherCircle.CentrePoint)); return(DistanceFromFloat <= CombinedRadius); }
public static bool LineIntersection(AABB Box, Vector3 StartPoint, Vector3 EndPoint, out Vector3 IntersectionPoint) { //Define initial lowest and highest values float Lowest = 0.0f; float Highest = 1.0f; //Default Value for intersection point IntersectionPoint = Vector3.zero; //Intersection Checks on every axis if (!IntersectingAxis(Vector3.right, Box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } if (!IntersectingAxis(Vector3.up, Box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } if (!IntersectingAxis(Vector3.forward, Box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } //Calculate Intersection point using interpolation //Not the only method IntersectionPoint = VectorMaths.VectorLerp(StartPoint, EndPoint, Lowest); return(true); }
//Physics actions public override void StateFixedActions() { if (m_patrollingNodes.Count != 0) { VectorMaths.RigidbodyXVelocity(m_rbCharacter, m_patrollingNodes[m_currentNode].transform.position - transform.position, m_forwardVelocity); } }
// Update is called once per frame void Update() { switch (TransType) { case 'P': gravityPosition.Position = gravityPos.actualposition; break; case 'M': gravityPosition.Position = gravityPosM.actualposition; break; } ShipPos = shippy.Transform.Position; Vector3 Direction = ShipPos - gravityPosition.Position; float Magnitude = VectorMaths.VectorMagnitude(ShipPos); if (Magnitude != 0) //Prevent dividing by 0 { RealGravityForce = GravityForce * (Distance / Magnitude); } shippy.Rig.force += Direction * RealGravityForce * Time.deltaTime; //Realistically this should only occur if the ship was within this range and if outside it would go the opposite direction //This doesn't matter in the content of this however. }
// Update is called once per frame void Update() { if (firsttime == false) { Transformation = GetComponent <myTransformation>(); //Gets the transformations Physics = GetComponent <MyPhysics>(); //Gets the physic Component Physics.Bouncy = true; //Sets the objects to be dynamic Physics.Mass = FindObjectOfType <Sniper>().Mass; //sets mass to 1.01f Physics.Force = new MyVector3(0, 0, 0); //Sets force to 0 Physics.Force += VectorMaths.EulerAnglesToDirection(new MyVector3(Transformation.Rotation.x, -Transformation.Rotation.y, 0)) * FindObjectOfType <Sniper>().FireSpeed; ForceApplied = Physics.Force; //Sets Force Applied to physics force firsttime = true; //Sets first time true Origin = Transformation.Translation; //Sets Origin to the position the object started at } Transformation.Scale = new MyVector3(20, 20, 20); //Sets the scale of the object Transformation = GetComponent <myTransformation>(); //Gets transformation component Physics = GetComponent <MyPhysics>(); //Gets the physic component DeltaTimer += Time.deltaTime; //Increase Delta Timer by delta time float Distance = (Origin - Transformation.Translation).Length(); //Distance from Origin to Object as length if (DeltaTimer > 60) { Destroy(this.gameObject); //Destroy the game object Destroy(this); //Destroy this component } if (Distance > 15000) { Destroy(this.gameObject); //Destroy the game object Destroy(this); //Destroy this component } }
public static bool LineIntersection(myAABB box, Vector3 StartPoint, Vector3 EndPoint, out Vector3 IntersectionPoint) { //define initial lowest and highest float Lowest = 0.0f; float Highest = 1.0f; //default value for intersection point is needed IntersectionPoint = Vector3.zero; //we do an intersection check on every axis by resuing the IntersectingAxis function if (!IntersectingAxis(Vector3.right, box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } if (!IntersectingAxis(Vector3.up, box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } if (!IntersectingAxis(Vector3.forward, box, StartPoint, EndPoint, ref Lowest, ref Highest)) { return(false); } //caulculate intersection point through interpolation IntersectionPoint = VectorMaths.LERP(StartPoint, EndPoint, Lowest); return(true); }
//overridden operator for multiplying two quaternions public static Quat operator *(Quat R, Quat S) { Quat RS = new Quat(0, Vector3.zero); RS.w = S.w * R.w - VectorMaths.DotProduct(S.GetAxis(), R.GetAxis()); RS.SetAxis(S.w * R.GetAxis() + R.w * S.GetAxis() + VectorMaths.CrossProduct(R.GetAxis(), S.GetAxis())); return(RS); }
// Update is called once per frame void Update() { Vector2 V = VectorMaths.SubtractVectors(Obj1, Obj2); MyDistance = Length(V); UnitiesDistance = V.magnitude; }
//A Segement start, B Segement end, C Other Point public static float SqDistanceFromPointToSegment(Vector3 A, Vector3 B, Vector3 C) { Vector3 AC = (C - A); Vector3 AB = (B - A); float squaredDistance = VectorMaths.LengthSq(AC) - (VectorMaths.DotProduct(AC, AB, false) * (VectorMaths.DotProduct(AC, AB, false)) / VectorMaths.LengthSq(AB)); return(squaredDistance); }
public float Bolttimer; //Timer for bolt void Start() { FireSpeed = 100; //Set firing speed to 100 Mass = 20; //Set mass to 20 ForwardDirection = VectorMaths.EulerAnglesToDirection(new MyVector2(0, 0)); //Convert pitch and yaw into a direction RightDirection = VectorMaths.VectorCrossProduct(MyVector3.up, ForwardDirection); //Get cross product of up and forward direction RightDirection = VectorMaths.VectorNormalized(RightDirection); //Normalise Right direction so if the player looks directly up they can still move left and right MassSlider.value = 1.01F; //Set mass to be 1.01 }
private static float distanceChecker(Vector3 spherePosition, Vector3 halfOfBoundary, Transform boundaries) { //calcuation for distance Vector3 distanceDifference = VectorMaths.vectorSubtraction3D(spherePosition, boundaries.position); float rightResult = Mathf.Pow(sqDistToBoundarie(distanceDifference, boundaries.right, halfOfBoundary.x), 2); float upResult = Mathf.Pow(sqDistToBoundarie(distanceDifference, boundaries.up, halfOfBoundary.y), 2); float leftResult = Mathf.Pow(sqDistToBoundarie(distanceDifference, boundaries.forward, halfOfBoundary.z), 2); return(Mathf.Sqrt(rightResult + upResult + leftResult)); }
//Physics actions public override void StateFixedActions() { VectorMaths.RigidbodyXVelocity(m_rbCharacter, transform.position - m_target.transform.position, m_forwardVelocity); if (m_rbCharacter.velocity.x > 0) m_enemyBase.m_characterModel.transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); else if (m_rbCharacter.velocity.x < 0) m_enemyBase.m_characterModel.transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f); }
void FixedUpdate() { ExplosionStrength = FindObjectOfType <ExplosionStrengthText>().slider.value; //Set Strength to be slider value ExplosionRadius = FindObjectOfType <ExplosionRadiusText>().slider.value; //Set radius to be slider value //THIS IS HOW YOU DO GRAVITY BUT OPPOSITE THIS REPELS THE OBJECTS ExplosionBounds = new BoundingCircle(Transformation.Translation, ExplosionRadius); //Set bounds of explosion Transformation = GetComponent <myTransformation>(); //Get Transformation component of object BoundingCircle Bounds = new BoundingCircle(new MyVector3(0, 0, 0), ExplosionRadius); //Bounds Of explosion Bounds = ExplosionBounds as BoundingCircle; //Set bounds to be xplosion bounds Bounds.CentrePoint = Transformation.Translation; //Make sure centre point is position of expplosion Object = FindObjectsOfType <MyPhysics>(); //Find all physic objects for (int i = 0; i < Object.Length; i++) { if (Bounds.Intersects(Object[i].Transformation.BoundObject)) { float Radius = float.MinValue; //Set radius to be minium value if (Object[i].Transformation.BoundObject is AABB) { AABB Box1 = Object[i].Transformation.BoundObject as AABB; //Get the object collision box if (Box1.MaxExtent.x > Radius) { Radius = Box1.MaxExtent.x; //Set radius to be max extent x } if (Box1.MaxExtent.y > Radius) { Radius = Box1.MaxExtent.y; //Set radius to be max extent y } if (Box1.MaxExtent.z > Radius) { Radius = Box1.MaxExtent.z; //Set radius to be max extent z } } if (Object[i].Transformation.BoundObject is BoundingCircle) { BoundingCircle Sphere1 = Object[i].Transformation.BoundObject as BoundingCircle; //Get the object bounding sphere Radius = Sphere1.Radius; //Set radius to be object radius } ExplosionForce = VectorMaths.VectorNormalized(Object[i].Transformation.Translation - Transformation.Translation) * ExplosionStrength; //Set force of explosion to be normalised of object postion - explosion position Object[i].Force += ExplosionForce; //Increase object force by explosion force CentreOfMass = Transformation.Translation; //SEt centre of mass to explosion point Distance = Object[i].Transformation.Translation - Transformation.Translation; //Distance from object to explosion Normalised = VectorMaths.VectorNormalized(Distance); //Normalised Distance ImpactPoint = Normalised * Radius; //Set impact point to be normalised * radius ExplosionTorque = VectorMaths.VectorCrossProduct(ExplosionForce, CentreOfMass - ImpactPoint); //Toorque os the cross product of explosion force and centre of mass - impact point Object[i].torque += ExplosionTorque; //Torque is increaess by explosion torque } } }
public static Quat ConvertToRad(Quat lhs) { Quat RET = new Quat(); RET.x = VectorMaths.Deg2Rad(lhs.x); RET.y = VectorMaths.Deg2Rad(lhs.y); RET.z = VectorMaths.Deg2Rad(lhs.z); RET.w = VectorMaths.Deg2Rad(lhs.z); return(RET); //Create RET }
public bool Intersects(BoundingSphere otherCircle) { //Square the sum of both radii float CombinedRadiusSq = (Radius + otherCircle.Radius) * (Radius + otherCircle.Radius); //Check if square distance is less than square radius, return result. //True means both objects have intersected // return false; return(VectorMaths.SqDistanceFromPointToSegment(A, B, otherCircle.CentrePoint3D) <= CombinedRadiusSq); }
public Vector128 <float> Normalize_Nested() { JohnVector x = _value; for (var i = 0; i < _iter; i++) { x = VectorMaths.Normalize3D(x); } return(x); }
public bool Intersects(BoundingSphere Comparison) //d2 <= (r1 + r2)2 Check if two objects are colliding { //Create a vector to represent the direction and length in comparison to the other circle Vector3 VectorToOther = Comparison.CentrePoint3D - CentrePoint3D; //Calculate the combined radio squared; float combinedRadiusSqrt = (Comparison.Radius + Radius); combinedRadiusSqrt *= combinedRadiusSqrt; return(VectorMaths.LengthSq(VectorToOther) <= combinedRadiusSqrt); }
private static float sqDistToBoundarie(Vector3 spherePosition, Vector3 boundariesLine, float tempHalfOfBoundary) { float excess = 0.0f; //calculates the boundaires Square distance if (VectorMaths.dotProduct3D(spherePosition, boundariesLine) < -tempHalfOfBoundary) { excess = VectorMaths.dotProduct3D(spherePosition, boundariesLine) + tempHalfOfBoundary; } else if (VectorMaths.dotProduct3D(spherePosition, boundariesLine) > tempHalfOfBoundary) { excess = VectorMaths.dotProduct3D(spherePosition, boundariesLine) - tempHalfOfBoundary; } return(excess); }
public MyVector3 GetMomentumAtPoint(MyVector3 point) { MyVector3 momentum = new MyVector3(); //Create momentum if (AngularVelocity.Length() > 0) { MyVector3 pointVelocity = Velocity + VectorMaths.VectorCrossProduct(AngularVelocity, Transformation.Translation - point); //Set point velocity to be velocity + the cross product of the angular velocity and translation - point momentum = Mass * pointVelocity; //Set momentum to be mass * point velocity } else { momentum = Mass * Velocity; //set momentum to be mass * velcity } return(momentum); //Return momentum }
private void Update() { frictionVector = velocity * friction; spherePosition += velocity = velocity -= frictionVector; //calculate the friction with the velocity to slow down the ball transform.position = spherePosition; foreach (Transform boundaires in allBoundaires) { //check though all the boudaries and see which one collides if (CollisionForBoundaries.collisionChecker(spherePosition, 1.0f, boundaires, boundaires.localScale / 2)) { velocity = VectorMaths.vectorReflectionAxisNotAligned(velocity, boundaires); /*set the velocity once reflexted*/ } } }
public Quat GetRotation() { //Convert Rotation to radians MyVector3 RotationInRadians = new MyVector3(); //Creates Rotation in radians RotationInRadians.x = VectorMaths.Deg2Rad(Rotation.x); //Converts Rotation x to Radians RotationInRadians.y = VectorMaths.Deg2Rad(Rotation.y); //Converts Rotation y to Radians RotationInRadians.z = VectorMaths.Deg2Rad(Rotation.z); //Converts Rotation z to Radians //Convert Rotation to Quat Quat RET = Quat.EulerToQuat(RotationInRadians); //Return rotation return(RET); //Create RET }
// Update is called once per frame void Update() { MyPhysics physics = GetComponent <MyPhysics>(); //Get and store the physics component timer += Time.deltaTime; //Add delta time to timer if (timer >= 3) { if (physics.Collided == true) { Alive = false; //Set Alive to falsez } } Transformation = GetComponent <myTransformation>(); //Get the object transformation and store it if (MyVector3.Length(Target - Transformation.Translation) < 5) { Transformation.Translation = Target; //Set translation to be target } else { if (MyVector3.Length(Target - Transformation.Translation) > 5) { Transformation.Translation = Target; //Set translation to be target } if (MyVector3.Length(Target - Transformation.Translation) > 50) { Speed = 5; //Speed set to 5 } if (MyVector3.Length(Target - Transformation.Translation) > 500) { Speed = 10; //Speed set to 10 } if (MyVector3.Length(Target - Transformation.Translation) > 5000) { Speed = 50; //Speed set to 50 } if (MyVector3.Length(Target - Transformation.Translation) > 50000) { Speed = 100; //Speed set to 100 } MyVector3 Direction = (Target - Transformation.Translation); //Direction = Target - Transllation Direction = VectorMaths.VectorNormalized(Direction); //Direction is equal to direction normalised MyVector3 Velocity = Direction * Speed; //Velocity = direction * speed Transformation.Translation += Velocity; //Add Velocity to current translation } }
public Vector3 GetMomentumAtPoint(Vector3 point) { Vector3 MomentumLocal; //If there is any angular velocity //Use your owN? if (AngularVelocity.magnitude > 0) { //Assume Position is centre of mass Vector3 pointVelocity = velocity + VectorMaths.VectorCrossProduct(AngularVelocity, MotherObject.Position - point); MomentumLocal = mass * pointVelocity; } else //Just calculate momentum using normal velocity { MomentumLocal = mass * velocity; } return(MomentumLocal); }
// Update is called once per frame void Update() { Vector3[] TransformedVertices = new Vector3[ModelSpaceVertices.Length]; angle += Time.deltaTime * 1; Rotation *= new Quat(EulerMaths.DegtoRag(RotationSpeed), VectorMaths.VectorNormalised(new Vector3(0, 1, 0))); for (int i = 0; i < TransformedVertices.Length; i++) { Quat p = new Quat(ModelSpaceVertices[i]); Quat newp = ((Rotation * p) * Rotation.Inverse()); Vector3 newpos = newp.GetAxisAngle(); TransformedVertices[i] = newpos; } MeshFilter MF = GetComponent <MeshFilter>(); MF.mesh.vertices = TransformedVertices; MF.mesh.RecalculateNormals(); MF.mesh.RecalculateBounds(); }
void RotatePlanet() //Same concept just x it by all the vertices e.t.c { Vector3[] TransformedVertices = new Vector3[ModelSpaceVertices.Length]; angle += Time.deltaTime * 50; Quat Rotation = new Quat(VectorMaths.DegToRad(angle), VectorMaths.VectorNormalize(new Vector3(1, 1, 1))); for (int i = 0; i < TransformedVertices.Length; i++) { Quat p = new Quat(ModelSpaceVertices[i]); Quat newp = ((Rotation * p) * Rotation.Inverse()); Vector3 newpos = newp.GetAxisAngle(); TransformedVertices[i] = newpos; } // TransformedVertices[0].x = -300; MeshFilter MF = GetComponent <MeshFilter>(); MF.mesh.vertices = TransformedVertices; MF.mesh.RecalculateNormals(); MF.mesh.RecalculateBounds(); }
//rotates the vertices of the planet model using the custom-made quaternions void Rotate() { Vector3[] TransformedVertices = new Vector3[ModelSpaceVertices.Length]; rotAngle += Time.deltaTime * rotSpeed * speedScalar; Quat Rotation = new Quat(VectorMaths.DegToRad(rotAngle), VectorMaths.VectorNormalize(new Vector3(1, 1, 1))); for (int i = 0; i < TransformedVertices.Length; i++) { Quat p = new Quat(ModelSpaceVertices[i]); Quat newp = ((Rotation * p) * Rotation.Inverse()); Vector3 newpos = newp.GetAxisAngle(); TransformedVertices[i] = newpos; } // TransformedVertices[0].x = -300; MeshFilter MF = GetComponent <MeshFilter>(); MF.mesh.vertices = TransformedVertices; MF.mesh.RecalculateNormals(); MF.mesh.RecalculateBounds(); }