Hot answers tagged

4

I suggest changing your API so that it tells you explicitly whether the lines intersect: public static bool LineIntersectionPoint(Vector2 startPoint1, Vector2 endPoint1, Vector2 startPoint2, Vector2 endPoint2, out Vector2 intersectionPoint) { // ... if (delta == 0) ...


3

You can return a Nullable Vector2? public static Vector2? LineIntersectionPoint(Vector2 startPoint1, Vector2 endPoint1, Vector2 startPoint2, Vector2 endPoint2) { // Get A,B,C of first line - points : startPoint1 to endPoint1 float A1 = endPoint1.y - startPoint1.y; float B1 = startPoint1.x - endPoint1.x; float C1 = A1 * startPoint1.x + B1 * ...


2

Here's a Unity-specific option in the same vein as sam hocevar's answer: Vector3 GetRandomUnitPerpendicular(Vector3 v) { float angle = Random.Range(0, Mathf.PI * 2f); // Generate a uniformly-distributed unit vector in the XY plane. Vector3 inPlane = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0f); // Rotate the vector into the plane ...


2

First , your polarToComponent(magnitude, angle) { return { x: magnitude * Math.cos(angle), y: magnitude * Math.sin(angle) }; Called for each ball at start time, introduce a numeric error. So you must calculate the total velocity before starting simulation and compare with that instead of 30. Second your function checkCollisions() ...


1

You are fixing the time to 10(seconds?), so it will always takke it 10 seconds to reach its target. So if its a short distance, the bullet will move very slow and over a long distance the bullet will move very fast. So you need to set the time to (distance)/(velocity).


1

You can't get the "right" vector from just a "forward" vector. Any particular "forward" vector could have an infinite number of different legal "up" and "right" vectors. For example, if I am looking forward along the z axis forwardVector = vec3(0,0,1), then I could have up be along the y axis upVector = vec3(0,1,0) and right therefore be along the x axis ...


1

I would do the following: have your vector u (ensure it’s normalised) pick an orthogonal vector v using any existing method (ensure it’s normalised) pick a random angle α a good random unit vector is therefore: v·cos(α) + (u × v)·sin(α) If you wish your vector to also have random length, you can then multiply it by a random number, or possibly by the ...



Only top voted, non community-wiki answers of a minimum length are eligible