Inefficient Hit Detection

I am trying to basically create a basketball game. To do this, I am modeling the trajectory of a ball based on kinematics equations because BodyVelocity seems to be very inconsistent. I am calculating the Vector3 position of the ball and updating it every frame.

local g = Vector3.new(0, -workspace.Gravity, 0) --gravity acceleration vector
local v --calculated in runtime

--Returns Vector3 position of ball over time
function x(t)
	return g*(t^2)/2 + v*t + initPos
end

I have successfully gotten the ball to go through the hoop with 100% accuracy, but now I am working on inaccurate shots—what happens when the player misses. Sometimes there is an airball, but sometimes the ball hits the rim. This means that I need to calculate the change in trajectory after the ball hits the rim. The rim is a UnionOperation part.

image

To detect collisions, I have created an invisible “detector” ball that is always rendered one frame ahead of the real game ball. This is how I currently check for collisions:

function checkForCollision(currPos, newPos)
	local touchingParts = touchDetector:GetTouchingParts()
	local norm = Vector3.new(0, 1, 0) --normal vector of top of target
	
	for _,part in pairs(touchingParts) do
		if part.Name == "Target" then
			targetHit = true --prevent multiple hit detections
			print("Target hit!")
		else
			ball.Position = newPos --continue along the original trajectory
		end
		break
	end
end

The problem is I do this every frame, which I feel is very inefficient. Furthermore, I would need to predict every single collision and change the ball’s trajectory accordingly. I have already tried raycasting to the next frame’s position, which didn’t work because the ray doesn’t account for the entire size of the ball. I don’t know of a better solution to this because of BodyVelocity’s inconsistency. Is there a better way to solve this problem?

A better approach would be to essentially decide the “fate” of the basketball before the trajectory is even calculated. Based on whatever variables you are using for the shot (power, accuracy, etc.), you can run a function once to determine the result. Then bases on that result, you will not need to calculate the trajectory or even detect collisions because you know where the basketball is headed and can just render the basketball at its current location based on offset time from shot time. In a render step bound loop you can then update the CFrame of the ball by setting it to the result of some function like the following:

function renderBasketball(curTime, trajectoryType)
    --Fill in details and return a CFrame
end

where:

  • curTime is the offset time from when the shot was taken and the current time in the place (you can use tick() or something like that)
  • trajectoryType may be some sort of custom Enum which may have values like “SuccessfulShot”, “RimBounce”, “Backboard”, “Airball”, etc…
1 Like

I was really intrigued by your problem but after realizing that you were using the quadratics I instantly remembered something we learned in school. It can be said that for any point on our trajectory can be described as

-ax^2+bx

Where a is like how “spacey” the trajectory is and b is how high it is in a sense. Take another, the point slope formula. It says that given 2 points we can describe a point. Now graph what I describe. You might get this

Notice how the calculator was able to figure out where that line interesected? How?

Solving a system of equation basically states given both of our graphs

red:
y=1(x+1)+0
purple:
-ax^2+bx

We can say that if there is a intersection of them it will be the values (or none at all) of x if we said

1(x+1)=-ax^2+bx

More over, if we define our graphs as this and rewrite them to something like, oh idk

red:
y=((p1.y - p2.y)/(p1.x - p2.x))*(x+p1.x)+p1.y
purple:
y=-ax^2+bx

And we putted this into a system of equations solver (i love mathpapa for this). Will end up with this mega equation that states for any given graph of both we can figure where x collides with (where p1.x = q, p1.y = w, p2.x = r, p2.y = t)

image

But why all of this? Well here’s where im getting at. Check if there is any solutions for this where the point slope graph is defined as the verticies of a object, each edge of it.

1 Like