How to reflect a balls velocity when it hits an obstacle

using applyimpulse

  1. What do you want to achieve? I’m trying to make a very bouncy ball with physics but every topic I look at uses ray casts

  2. What is the issue? I dont know how to do this

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    everything that i saw on the developer hub about this used raycasts
    also i am new so if you can try to simplify it

6 Likes

I would probably use a part.touched function (if you don’t want to use ray-casts)

	local ball = script.Parent

	ball.Touched:Connect(function(hitPart)
		local wall = hitPart
		
		-- Create dictionary that stores the difference angle
		
		local orientationDiffer = {x = (wall.Orientation.X - ball.Orientation.X), y = (wall.Orientation.Y - ball.Orientation.Y), z = (wall.Orientation.Z - ball.Orientation.Z)}
		
		ball.Orientation.X = (orientationDiffer.x - 180) + (orientationDiffer.x * 2)
		ball.Orientation.Y = (orientationDiffer.y - 180) + (orientationDiffer.y * 2)
		ball.Orientation.Z = (orientationDiffer.z - 180) + (orientationDiffer.z * 2)
	end)

Btw this probably doesn’t work cuz I’m not very good at math

1 Like

may i ask what is the if statement for also what is the part variable supposed to be (asking this because the original script i used was inside the ball)

1 Like

I can modify it to work for the ball (that actually makes much more sense)

1 Like

I dont need an actual script rather I need an example because I want to learn

1 Like

So basically, when a ball hits a wall it bounces off in the opposite direction. You would probably just want to add 90 degrees to the ball orientation:

45 degrees → 135 degrees

which actually works for this situation but if you have something like:

10 degrees → 100 degrees

it would look like the ball is hitting an invisible wall facing the incorrect direction. So you have to find the difference between the angle of the ball and the angle of the wall:

wall: (0 degrees) ball: (45 degrees) difference: (45 degrees)

next add 180 degrees to the difference:

difference + 180 = 225

now it would look like the ball is passing straight through the wall so you have to do:

225 - (difference * 2) = 135

and now you have your reflected value (for one axis). Here it is in one math equation:

ballReflectedDirection = ((wallDirection - BallDirection) + 180) - (difference * 2)

(gtg so prob no gonna respond for about 3 hours)

Shouldn’t it use velocity instead of orientation?

Yeah, idk if this will work but you can try and i’m definitely not effbeecee

It only orients the part to the angle

its a projectile not a raycast

I’ll make a velocity thingy one sec

local ball = script.Parent

local speed = 15

local debounce = false

-- Ball move script

local function MoveBall(speed)
	ball:ApplyImpulse(ball.CFrame.LookVector * speed)
end

ball.Touched:Connect(function(hitPart)
	if debounce == false then
		debounce = true

		local wall = hitPart

		-- Slow ball down by 1 stud per second
		speed -= 1

		-- Create dictionary that stores the difference angle

		local orientationDiffer = {x = (wall.Orientation.X - ball.Orientation.X), y = (wall.Orientation.Y - ball.Orientation.Y), z = (wall.Orientation.Z - ball.Orientation.Z)}

		ball.Orientation = Vector3.new(
			(orientationDiffer.x - 180) + (orientationDiffer.x * 2),
			(orientationDiffer.y - 180) + (orientationDiffer.y * 2),
			(orientationDiffer.z - 180) + (orientationDiffer.z * 2)) 

		-- Moves ball away from part
		MoveBall(10)

		debounce = false
	end
end)

It’s CFrame though? by velocity I mean AssemblyLinearVelocity and :ApplyImpulse()

Now that I think about it, it’s probably easier to learn about ray-casts than to use this as it’s very difficult to find which side was hit without using 6 hitboxes (which just complicates it way too much).

How do I put raycasts in a weapon that uses roblox physics? Basically I’m trying to make SCP 018 but it just flings out of the map or gets stuck in the floor at one point even if I limit the speed

i dont know if i can bump but i dont want to create a new post either

Raycasts are used by doing workspace:Raycast() so they technically aren’t anywhere they just appear whenever you use that function. For a weapon you could just raycast at the position of the weapon in the direction it’s facing.