Back to home

Ray Sphere Intersection Function (Demonstration)

Z4EBY
click to copy
This game is archived.
If you with to unarchive it, click the "restore" button in the options menu.

Category:
Other

Author:
BestLizard

Rating:
0

Version:
1.0.0

Posted:
17 days ago

Tags:
function sub-routine helpful library utility

Share this workshop!

Options

IF YOU USE THE FUNCTION, PLEASE KEEP CREDIT TO ME IN THE RULE NAME!!!

(This code has been written for the PTR version. It will be updated once functions, loops, etc. come to the live version of Overwatch)

Ray-Sphere Intersection

All input and output go through a single global variable. By default, global variable 3 is used, but this can be easily changed for your game mode by pasting the subroutine itself into a text editor. This global variable is an array, with indices 0-3 being the input, and 4-9 being the output.

I/O Breakdown

Inputs

0: Ray origin (ie eye position) 1: Ray direction (ie facing direction of) 2: Sphere Centre 3: Sphere Radius

Outputs

4: If ray intersects sphere (true if hit) 5: If sphere is behind player (true if sphere is entirely behind player. False if player is in sphere, or if sphere is ahead of player. Calculated as a courtesy) 6: Intersection Distance 1 (distance along the ray from origin to first intersection with sphere) 7: Intersection Distance 2 (distance along the ray to second intersection with sphere) 8: Intersection Position 1 (the first exact location an intersection occurs. Calculated as a courtesy) 9: Intersection Position 2 (the second exact location an intersection occurs. Calculated as a courtesy)

NOTE: Outputs 5-9 have GARBAGE if no intersection occurs with the ray. Always check index 4 after this function completes!

Demonstration

The game mode settings and additional rules are included in this import code to demonstrate usage of the function. The snippet on this site contains only the ray-intersection function: paste that into your game mode if you want to use it. Feel free to use it in any game mode, just please keep credit to me in the rule name.

variables
{
	global:
		3: raySphereIntersectionIO
}

subroutines
{
	0: raySphereIntersection
}

rule("FUNCTION Ray Sphere Intersection (By: BestLizard#1874 / (Discord) Lizard Leliel#7967)")
{
	event
	{
		Subroutine;
		raySphereIntersection;
	}

	actions
	{
		"Calculate vector from sphere to ray origin"
		Set Global Variable At Index(raySphereIntersectionIO, 8, Subtract(Value In Array(Global Variable(raySphereIntersectionIO), 0),
			Value In Array(Global Variable(raySphereIntersectionIO), 2)));
		"Calculate "a" in quadratic formula"
		Set Global Variable At Index(raySphereIntersectionIO, 9, Dot Product(Value In Array(Global Variable(raySphereIntersectionIO), 1),
			Value In Array(Global Variable(raySphereIntersectionIO), 1)));
		"Calculate "b" in Quadratic Formula"
		Set Global Variable At Index(raySphereIntersectionIO, 10, Multiply(2, Dot Product(Value In Array(Global Variable(
			raySphereIntersectionIO), 8), Value In Array(Global Variable(raySphereIntersectionIO), 1))));
		"Calculate "c" in quadratic formula"
		Set Global Variable At Index(raySphereIntersectionIO, 11, Subtract(Dot Product(Value In Array(Global Variable(
			raySphereIntersectionIO), 8), Value In Array(Global Variable(raySphereIntersectionIO), 8)), Multiply(Value In Array(
			Global Variable(raySphereIntersectionIO), 3), Value In Array(Global Variable(raySphereIntersectionIO), 3))));
		"Caluclate discriminant"
		Set Global Variable At Index(raySphereIntersectionIO, 12, Subtract(Multiply(Value In Array(Global Variable(
			raySphereIntersectionIO), 10), Value In Array(Global Variable(raySphereIntersectionIO), 10)), Multiply(4, Multiply(
			Value In Array(Global Variable(raySphereIntersectionIO), 9), Value In Array(Global Variable(raySphereIntersectionIO), 11)))));
		"The ray does NOT intersect"
		If(Compare(Value In Array(Global Variable(raySphereIntersectionIO), 12), <, 0));
			Set Global Variable At Index(raySphereIntersectionIO, 4, False);
			Set Global Variable At Index(raySphereIntersectionIO, 5, Null);
			Set Global Variable At Index(raySphereIntersectionIO, 6, Null);
			Set Global Variable At Index(raySphereIntersectionIO, 7, Null);
			Abort;
		End;
		"Ray does intersect; compute if its behind or not, intersection distances, and intersection locations"
		Set Global Variable At Index(raySphereIntersectionIO, 4, True);
		Set Global Variable At Index(raySphereIntersectionIO, 6, Divide(Subtract(Multiply(-1.000, Value In Array(Global Variable(
			raySphereIntersectionIO), 10)), Square Root(Value In Array(Global Variable(raySphereIntersectionIO), 12))), Multiply(2,
			Value In Array(Global Variable(raySphereIntersectionIO), 9))));
		Set Global Variable At Index(raySphereIntersectionIO, 7, Divide(Add(Multiply(-1.000, Value In Array(Global Variable(
			raySphereIntersectionIO), 10)), Square Root(Value In Array(Global Variable(raySphereIntersectionIO), 12))), Multiply(2,
			Value In Array(Global Variable(raySphereIntersectionIO), 9))));
		Set Global Variable At Index(raySphereIntersectionIO, 5, And(Compare(Value In Array(Global Variable(raySphereIntersectionIO), 6),
			<, 0), Compare(Value In Array(Global Variable(raySphereIntersectionIO), 7), <, 0)));
		Set Global Variable At Index(raySphereIntersectionIO, 8, Add(Value In Array(Global Variable(raySphereIntersectionIO), 0), Multiply(
			Value In Array(Global Variable(raySphereIntersectionIO), 6), Value In Array(Global Variable(raySphereIntersectionIO), 1))));
		Set Global Variable At Index(raySphereIntersectionIO, 9, Add(Value In Array(Global Variable(raySphereIntersectionIO), 0), Multiply(
			Value In Array(Global Variable(raySphereIntersectionIO), 7), Value In Array(Global Variable(raySphereIntersectionIO), 1))));
	}
}