void Update() { if (Input.GetKeyDown(KeyCode.Mouse0)) { PositionBegin = Input.mousePosition; } else if (Input.GetKeyUp(KeyCode.Mouse0)) { arrow.SetArrow(0f, 0f); PositionEnd = Input.mousePosition; float x = PositionEnd.x - PositionBegin.x; float z = PositionEnd.y - PositionBegin.y; float dis = Mathf.Sqrt(x * x + z * z); if (dis > MinDis) { player.AddForce(x, z); gameObject.SetActive(false); } } else if (Input.GetKey(KeyCode.Mouse0)) { PositionEnd = Input.mousePosition; float angle = Vector3Extension.AngleRelative(PositionBegin, PositionEnd); float distance = Vector3.Distance(PositionEnd, PositionBegin); arrow.SetArrow(angle, distance * 0.01f); } }
//private void DoMirror(Stretch st, Node n, Vector3 previousPos, Vector3 newPos) //{ // Vector3 a = previousPos - n.transform.position; // Vector3 b = newPos - n.transform.position; // Vector3 axis = Vector3.Cross(a, b); // float deltaAngle = Vector3.SignedAngle(a, b, axis); // foreach (Stretch s in creator.GetStretches(n)) // { // if (s != st) // { // switch (s.IsAnchorA(n)) // { // //Is ControlA // case true: // s.ControlA = Vector3Extension.RotatePointAroundAxisPivot(s.ControlA, n.transform.position, axis, deltaAngle); // break; // //Is ControlB // case false: // s.ControlB = Vector3Extension.RotatePointAroundAxisPivot(s.ControlB, n.transform.position, axis, deltaAngle); // break; // default: // Debug.Assert(true); // break; // } // s.OnPathModified(); // } // } //} private void DoMirrorPairs(Stretch st, Node n, Vector3 previousPos, Vector3 newPos) { Stretch[] stretches = creator.GetStretches(n); if (stretches.Length == 2) { //Vector3 a = previousPos - n.transform.position; //Vector3 b = newPos - n.transform.position; //Vector3 axis = Vector3.Cross(a, b); //float deltaAngle = Vector3.SignedAngle(a, b, axis); for (int i = 0; i < stretches.Length; i++) { Stretch s = stretches[i]; if (s != st) { if (s.IsAnchorA(n)) { s.ControlA = Vector3Extension.ReflectWithPoint(newPos, n.Pos); } else { s.ControlB = Vector3Extension.ReflectWithPoint(newPos, n.Pos); } } } } }
private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.layer == 8) { rb.velocity = Vector3Extension.CalculateDirectionTowardsMouse(transform.position); } }
public override bool OnCalloutAccepted() { Game.LogTrivial("UnitedCallouts Log: GangAttack callout accepted."); Game.DisplayNotification("web_lossantospolicedept", "web_lossantospolicedept", "~w~UnitedCallouts", "~y~Gang Attack", "~b~Dispatch: ~w~The gang members are marked on the map. Respond with ~r~Code 3"); _AG1 = new Ped("g_m_y_ballasout_01", Vector3Extension.ExtensionAround(_SpawnPoint, 20f), 0f); _AG2 = new Ped("g_m_y_ballasout_01", Vector3Extension.ExtensionAround(_SpawnPoint, 30f), 0f); _AG3 = new Ped("g_m_y_ballasout_01", Vector3Extension.ExtensionAround(_SpawnPoint, 22f), 0f); _AG4 = new Ped("g_m_y_famca_01", Vector3Extension.ExtensionAround(_SpawnPoint, 24f), 0f); _AG5 = new Ped("g_m_y_famca_01", Vector3Extension.ExtensionAround(_SpawnPoint, 26f), 0f); _AG6 = new Ped("g_m_y_famca_01", Vector3Extension.ExtensionAround(_SpawnPoint, 28f), 0f); _AG1.Inventory.GiveNewWeapon("WEAPON_MICROSMG", 5000, true); _AG2.Inventory.GiveNewWeapon("WEAPON_PISTOL", 5000, true); _AG3.Inventory.GiveNewWeapon("WEAPON_MICROSMG", 5000, true); _AG4.Inventory.GiveNewWeapon("WEAPON_MICROSMG", 5000, true); _AG5.Inventory.GiveNewWeapon("WEAPON_PISTOL", 5000, true); _AG6.Inventory.GiveNewWeapon("WEAPON_PISTOL", 5000, true); _Blip = _AG1.AttachBlip(); _Blip2 = _AG2.AttachBlip(); _Blip3 = _AG3.AttachBlip(); _Blip4 = _AG4.AttachBlip(); _Blip5 = _AG5.AttachBlip(); _Blip6 = _AG6.AttachBlip(); _Blip.EnableRoute(Color.Yellow); if (Settings.ActivateAIBackup) { Functions.RequestBackup(_SpawnPoint, LSPD_First_Response.EBackupResponseType.Code3, LSPD_First_Response.EBackupUnitType.LocalUnit); Functions.RequestBackup(_SpawnPoint, LSPD_First_Response.EBackupResponseType.Code3, LSPD_First_Response.EBackupUnitType.LocalUnit); } return(base.OnCalloutAccepted()); }
private double ComputeError(int id1, int id2, out Vector3 error) { ErrorMetric edge = ErrorMetric.Empty; ErrorMetric delta = ErrorMetric.Empty; Vector3 vertex = new Vector3(); edge = m_Quadrics[id1] + m_Quadrics[id2]; delta = edge; Matrix m = delta.Matrix; { m.M41 = 0.0f; m.M42 = 0.0f; m.M43 = 0.0f; m.M44 = 1.0f; } delta.Matrix = m; double minError = 0.0; if (Math.Abs(delta.Matrix.Determinant()) <= 1e-5) { Vector3 v1 = m_Vertices[id1].Position; Vector3 v2 = m_Vertices[id2].Position; Vector3 v3 = Vector3Extension.Mean(v1, v2); double e1 = edge.Evaluate(v1); double e2 = edge.Evaluate(v2); double e3 = edge.Evaluate(v3); minError = Math.Min(Math.Min(e1, e2), e3); if (minError == e1) { vertex = v1; } else if (minError == e2) { vertex = v2; } else if (minError == e3) { vertex = v3; } } else { double det = (double)delta.Matrix.IndexedDeterminant(0, 1, 2, 4, 5, 6, 8, 9, 10); vertex.X = (float)(-1.0 / det * (delta.Matrix.IndexedDeterminant(1, 2, 3, 5, 6, 7, 9, 10, 11))); vertex.Y = (float)(1.0 / det * (delta.Matrix.IndexedDeterminant(0, 2, 3, 4, 6, 7, 8, 10, 11))); vertex.Z = (float)(-1.0 / det * (delta.Matrix.IndexedDeterminant(0, 1, 3, 4, 5, 7, 8, 9, 11))); } error = vertex; minError = edge.Evaluate(vertex); return(minError); }
/// <summary> /// Calculates the area of the triangle defined by the points p0, p1 and p2. /// </summary> public static float TriangleArea(Vector3 p0, Vector3 p1, Vector3 p2) { float a = Vector3Extension.SqrDistance(p0, p1); float b = Vector3Extension.SqrDistance(p1, p2); float c = Vector3Extension.SqrDistance(p2, p0); return(Mathf.Sqrt((2 * a * b + 2 * b * c + 2 * c * a - a * a - b * b - c * c) / 16)); }
public static Vector3 Around(this Vector3 start, float radius) { // Random direction. Vector3 direction = Vector3Extension.RandomXY(); Vector3 around = start + (direction * radius); return(around); }
private void StartCharge() { Time.timeScale = 1f; rb.velocity = Vector3Extension.CalculateDirectionTowardsMouse(transform.position) * chargeSpeed; StartCoroutine("ChargeDuration", abilityDuration); anim.SetBool("Charge", true); isCharging = true; ArenaEvents.PlayerCharge(); hasCharged = false; }
private void FallDawn() { if (isFalling == true) { transform.position = Vector3.MoveTowards(transform.position, Vector3Extension.MousePosition(), fallSpeed * Time.deltaTime); if (0.01f > Vector3Extension.DistanceBetweenPlayerMouse(transform.position, Vector3Extension.MousePosition())) { PoundAttack(); } } }
private void OnDrawGizmos() { Gizmos.color = Color.yellow; for (float x = 0; x < 10; x += size) { for (float y = 0; y < 10; y += size) { var point = GetNearestPointOnGrid(Vector3Extension.AsVector2(new Vector3(x, y, 0f))); Gizmos.DrawSphere(point, 0.1f); } } }
private void Jump() { PlayerUI.instance.Used1Ability(); transform.position = new Vector3(Vector3Extension.MousePosition().x, transform.position.y + 1.7f, transform.position.z); Time.timeScale = 1f; GetComponent <ArenaMovement>().enabled = false; isFalling = true; anim.SetBool("FallAttack", true); anim.SetBool("Run", false); anim.SetBool("Idle", false); preparedToJump = false; }
private void CharacterRotation() { if (Vector3Extension.MousePosition().x > transform.position.x) { transform.rotation = Quaternion.Euler(0, 0, 0); transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, 1); } else { transform.rotation = Quaternion.Euler(0, 180, 0); transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, -1); } }
private void AnimationsControll() { if (0.01f < Vector3Extension.DistanceBetweenPlayerMouse(transform.position, Vector3Extension.MousePosition())) { anim.SetBool("Run", true); anim.SetBool("Idle", false); } else { anim.SetBool("Run", false); anim.SetBool("Idle", true); } }
void MoveTowardsMouse() { if (canMove == true) { transform.position = Vector3.MoveTowards(transform.position, Vector3Extension.MousePosition(), moveSpeed * Time.deltaTime); CharacterRotation(); } MovementBoundsToScreen(); AnimationsControll(); }
public Vector2 GetNearestPointOnGrid(Vector2 position) { Debug.Log("Hello: " + position); position -= Vector3Extension.AsVector2(transform.position); int xCount = Mathf.RoundToInt(position.x / size); int yCount = Mathf.RoundToInt(position.y / size); Vector2 result = new Vector2( (float)xCount * size, (float)yCount * size); result += Vector3Extension.AsVector2(transform.position); Debug.Log(result); return(result); }
private void Dash() { if (Input.GetKeyDown(KeyCode.Mouse0) && timer > dashCooldown && !isDashing) { isDashing = true; anim.SetTrigger("Punch"); timer = 0; GetComponent <ArenaMovement>().canMove = false; } if (isDashing == true) { float step = (1f * dashSpeed) * Time.fixedDeltaTime; transform.position = Vector3.MoveTowards(transform.position, Vector3Extension.MousePosition(), step / dashRange); transform.position.Normalize(); } else { timer += Time.fixedDeltaTime; } }
/// <summary> /// Calculates the ray to AABB intersections. /// </summary> public static bool IntersectRayAABB(Ray ray, Bounds bounds, out float tEntry, out Vector3 pointEntry, out float tExit, out Vector3 pointExit) { Vector3 rayDirectionInverse = Vector3Extension.Reciprocal(ray.direction); // Perform ray-slab intersection (component-wise). Vector3 t0 = Vector3.Scale(bounds.min, rayDirectionInverse) - Vector3.Scale(ray.origin, rayDirectionInverse); Vector3 t1 = Vector3.Scale(bounds.max, rayDirectionInverse) - Vector3.Scale(ray.origin, rayDirectionInverse); // Find the closest/farthest distance (component-wise). Vector3 tSlabEntry = Vector3.Min(t0, t1); Vector3 tSlabExit = Vector3.Max(t0, t1); // Find the farthest entry and the nearest exit. tEntry = Mathf.Max(tSlabEntry.x, tSlabEntry.y, tSlabEntry.z); tExit = Mathf.Min(tSlabExit.x, tSlabExit.y, tSlabExit.z); // Calculate the points. pointEntry = ray.origin + ray.direction * tEntry; pointExit = ray.origin + ray.direction * tExit; return(tEntry < tExit); }
public static int GetSize() { return(Vector3Extension.SizeOf() * 4 + sizeof(int) * 3 + sizeof(float) * 2); }
void HandleCollision(RaycastHit hit) { //Debug distance and time //print((transform.position - startPos).magnitude + " t: " + t); //If projectile is too slow, kill it if (velocity.sqrMagnitude < 5) { Die(); return; } //We collided with something, say it to debugger //wall = true; //Get all hits (>1, if projectile is too fast and walls count != 1 too) RaycastHit[] hits = Physics.RaycastAll(prevPos, transform.position - prevPos, (transform.position - prevPos).magnitude); float c = 0; //Foreach every hit in hits while (c < hits.Length) { //Count start energy float E1 = mass * velocity.sqrMagnitude / 2; //Angle of bullet to wall float angle = Vector3.Angle(hit.normal, velocity) - 90; //Get maximum wall width float maxWidth = (E1 / hardness / 100); //Get point, where we can see, if we penetrated the wall Vector3 checkPoint = hit.point + velocity.normalized * maxWidth; DoDamage(hit, velocity);//Apply Damage RaycastHit newHit; //If there is point, so wall is penetrated if (Physics.Raycast(checkPoint, -velocity, out newHit, maxWidth)) { float width = (newHit.point - hit.point).magnitude * 100; //Count real wall width float EP = width * hardness; //Count Enegy loss float E2 = E1 - EP; //Find difference //Save it so we dont need to count it every time float projectileMultiplier = 1 / velocity.magnitude * width / mass; //Make sure E2 is greater than 0 if (E2 < 0) { Die(); return; } //OK penetrating further Vector3 n = new Vector3(0, hit.normal.y, 0); //Get wall normal's Y component //Count new velocity (mostly empirical) velocity = velocity.normalized * Mathf.Sqrt((2 / mass) * E2) - n * projectileMultiplier * Random.Range(1, 2f) * 25 + Random.insideUnitSphere * Random.Range(0f, 1f) * projectileMultiplier * hardness * hardness / 500; checkPoint = hit.point + velocity.normalized * maxWidth; //Recalculate check point Physics.Raycast(checkPoint, -velocity, out newHit, maxWidth); //Find REAL penetration point transform.position = newHit.point; //Move projectile to this point //DEBUG// Debug.DrawLine(newHit.point, hit.point, Color.red, 1); } //Else reflect if RANDOM allows (empirical) //TODO: remake reflect system else if (Random.Range(0f, 1f) / angle / Mathf.Deg2Rad * (hardness) * section * 120 > 1) { transform.position = hit.point;//Move projectile to hit point //Count vertical angle float newAngle = Mathf.Clamp(angle * Mathf.Deg2Rad * Random.Range(1.3f, 2.2f), 0, Mathf.PI / 2f); //Count horizontal angle float randAngle = Random.Range(-1f, 1f) / 100 / mass * angle * angle * Mathf.Deg2Rad * Mathf.Deg2Rad + Mathf.PI / 2; //Reflect velocity by angles velocity = Vector3Extension.RandomReflectAngle(velocity, hit.normal, newAngle, randAngle) / (1 + angle * Mathf.Deg2Rad); //Normal correction velocity *= Mathf.Cos(angle * Mathf.Deg2Rad) * Mathf.Cos(angle * Mathf.Deg2Rad); if (velocity.sqrMagnitude < 5) { Die(); return; } return; } else//didn't ricochet, didn't penetrate { Die(); return; } //Check if there are other hits if (hits.Length > 1) { Physics.Raycast(newHit.point, velocity, out hit, 5); //DEBUG// Color lerped = Color.Lerp(Color.green, Color.red, velocity.magnitude / startSpeed); //DEBUG// Debug.DrawLine(newHit.point, hit.point, lerped,1); } c++; } }
public static void AreEqual(Vector3 expected, Vector3 actual, float deltaSquared = 1e-8f) { Assert.IsTrue(Vector3Extension.SquaredDistance(expected, actual) < deltaSquared, "Vector3's are not equal: expected={0}; actual={1}; deltaSquared={2}", expected.ToString("F8"), actual.ToString("F8"), deltaSquared); }
// Update is called once per frame void Update() { float step = timespeed * Time.deltaTime; float xpos = Input.mousePosition.x; float ypos = Input.mousePosition.y; mousePosLast = mousePos; mousePos = cam.ScreenToWorldPoint(new Vector3(xpos, ypos, cam.nearClipPlane)); // calk velocity of input mouseVelLast = mouseVel; mouseVel = (mousePos - mousePosLast) / step; // calk accelleration of input mouseAccel = (mouseVel - mouseVelLast) / step; // speed, acceleration, force friction are guing into physical model // we need to split acceleration components in axis of car (CoG) // we do that by rotating the axis of the applied force by the amount of the car.rotation // so the components are aligned and othogonal with the car // angel of car = -atan2(Rx,Ry) // found to be empirically correct (negative because 3d z-axis) cartopullerrel = puller.position - car.position; angle = -Mathf.Atan2(cartopullerrel.x, cartopullerrel.y); // Get acceleration component in car koordinates accelComponents = Vector3Extension.Rotate(mouseAccel, -angle); // calk car force F=m*a (y because of car initial orientation pointing up) float carforce = mass * accelComponents.y; carAccel = Vector3Extension.Rotate(new Vector3(0, carforce, 0), angle); // rotate acceleration back to world coordinates // integrate force to get velocity carVel.x = (carAccel.x / mass) * step + carVel.x; carVel.y = (carAccel.y / mass) * step + carVel.y; // constrain to mouse velocity in y axis(car) Vector3 carvelComponents = Vector3Extension.Rotate(carVel, -angle); Vector3 mousvelComponents = Vector3Extension.Rotate(mouseVel, -angle); carvelComponents.y = mousvelComponents.y; // reduce velocity by friction linearly in x axis(car) if (carvelComponents.x > friction * step) { carvelComponents.x -= friction * step; } else if (carvelComponents.x < -friction * step) { carvelComponents.x += friction * step; } else if ((carvelComponents.x < friction * step && carvelComponents.x > 0) || (carvelComponents.x > -friction * step && carvelComponents.x < 0)) { carvelComponents.x = 0; } // friction is overwritten by mouse in y // convert new velocity to world koordinates carVel = Vector3Extension.Rotate(carvelComponents, angle); // integrate velocity to get position carPos.x = carVel.x * step + carPos.x; carPos.y = carVel.y * step + carPos.y; // set car to distance offset from puller Vector3 pullertocar = carPos - puller.position; pullertocar.Normalize(); pullertocar = pullertocar * caroffset; // TODO: this is still not working as expected // set car position to carPos = pullertocar + puller.position; //update the position of puller (under the mouse) puller.position = cam.ScreenToWorldPoint(new Vector3(xpos, ypos, cam.nearClipPlane)); // update rotation (always pointing to puller) Quaternion tempquatrotate = Quaternion.Euler(0, 0, angle * Mathf.Rad2Deg); car.rotation = tempquatrotate; // update position of car float maxdistance = 2; car.position = Vector3.MoveTowards(car.position, carPos, maxdistance); }
void Update() { // reset axis circularAxis = 0; straightAxis = 0; straightAxisAngle = 0; if (Input.GetMouseButtonDown(0)) { touchPositions = new List <Vector3>(); touchPositions.Add(Input.mousePosition); } // Continuous circular detection if (Input.GetMouseButton(0) && touchPositions.Count >= 1) { // calculate distance between last 2 points float distance = Vector3.Distance(Input.mousePosition, touchPositions[touchPositions.Count - 1]); // calculate whether it's a relatively short or a long distance, so we can include more/less points float distanceFactor = 1 - Mathf.Clamp01((Mathf.Abs(distance) - minDistanceBetweenPoints) / (maxDistanceBetweenPoitns - minDistanceBetweenPoints)); int pointSampleSize = (int)(distanceFactor * (maxPointSampleSize - minPointSampleSize) + minPointSampleSize); Debug.Log(distanceFactor + " " + pointSampleSize); // check if minimum distance is met if (distance > minDistanceBetweenPoints) { // add current touch touchPositions.Add(Input.mousePosition); // make sure we have enough points to do detection if (touchPositions.Count > 2) { // measure current and last angles var currentAngle = Vector3Extension.Angle2D(touchPositions[touchPositions.Count - Mathf.Min(touchPositions.Count, (int)pointSampleSize)], touchPositions[touchPositions.Count - 1]); var lastAngle = Vector3Extension.Angle2D(touchPositions[touchPositions.Count - Mathf.Min(touchPositions.Count, (int)pointSampleSize)], touchPositions[touchPositions.Count - 2]); var anglediff = Mathf.DeltaAngle(currentAngle, lastAngle); // if the difference is over the threshold if (Mathf.Abs(anglediff) > minAngleToDetectTurn) { // subtract threshold !!! not sure if needed -joon if (anglediff > 0) { anglediff -= minAngleToDetectTurn; } if (anglediff < 0) { anglediff += minAngleToDetectTurn; } // set axis, so it can be read circularAxis = Mathf.Clamp(anglediff, -angleToDetectTurnClamp, angleToDetectTurnClamp); // add debug GL lines if (debugCircularEnabled) { if (circularAxis > 0) { DebugCameraLines.AddLine(touchPositions[touchPositions.Count - (int)pointSampleSize], touchPositions[touchPositions.Count - 1], Color.red); } else { DebugCameraLines.AddLine(touchPositions[touchPositions.Count - (int)pointSampleSize], touchPositions[touchPositions.Count - 1], Color.blue); } } } else { // set straight axis straightAxis = distance; straightAxisAngle = lastAngle; //Debug.Log(straightAxis + " " + straightAxisAngle); if (debugCircularEnabled) { DebugCameraLines.AddLine(touchPositions[touchPositions.Count - (int)pointSampleSize], touchPositions[touchPositions.Count - 1], Color.yellow); } } if (debugCircularEnabled) { DebugCameraLines.AddLine(touchPositions[touchPositions.Count - (int)pointSampleSize], touchPositions[touchPositions.Count - 2], Color.white.SetA(0.4f)); } } } } }
public float RootToCenterDistance() { return(this._targetToDistance * Vector3Extension.GetMaxFloat(base.transform.localScale)); }