Fixing ball not bouncing off wall

I have a problem with my physics system, the ball only bounces off one wall, while sticking to the other, and im not sure how to fix it.

First wall:


Second wall:

Here’s the part of the script that does the wall-bouncing:

local rayDir = vel.Unit*Ball.Size.Y/2
		local movementRay = workspace:Raycast(pos,rayDir,params)
		if movementRay then
			local normal =movementRay.Normal
			if normal == Vector3.new(0,1,0) then
				vel = Vector3.new(vel.X,-vel.Y*BounceAmount,vel.Z)
			else
				vel *= -normal
			end
			--func:PlaySound(SoundService.SFX.BallGround,.5,Ball)
		end
1 Like

Try setting the material to rubber and setting CustomPhysicalProperties to false. This should make it have the physics properties of rubber.

2 Likes

I made my own custom physics system, vel is velocity

2 Likes

Im assuming you mean the second time it bounces by “second wall”. I’m on mobile and can’t test anything, but would resetting each variable work? Also, could we see the code that detects the wall touch?

1 Like

Nah, by second wall i mean a whole different wall

1 Like

Is it just that second wall, or is it every wall except for the one that works? If it is just the first wall that works, then your variables might not be in relation to the wall’s cframe, in which case you might want to use cframe.lookvector. Sorry if this wrong or sounds stupid, but this is the best answer I can give lol

1 Like

Im using ray.normal to get the reflected velocity, am i doing something wrong?

1 Like

The problem might be with your ray cast params. Try debugging using print after your if statement regarding your normal. Also, don’t assume I am correct in this, I’m a beginner scripter and can’t test any code right now, so I am offering possible causes for the problem.

1 Like


nope

1 Like

For more context, here’s the whole thing:


(changing a bit but it’s not working still)

1 Like

The issue is that the formula you’ve used does not actually result in a reflected vector. We need to use the dot product (not the Hadamard product) of the ball’s velocity and the surface’s normal in order to properly reflect the velocity vector.

outVelocity = (inVelocity - (2 * (inVelocity:Dot(normal)) * normal)) * BOUNCINESS
3 Likes

Wow thank you so much this instantly fixed it

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.