X and z velocity for custom physics[HELP NEEDED]

Hello! I am wondering how to make a curved trajectory for a ball while using custom physics because when I try to it does an upside-down V shape instead. I am trying to avoid bezier curves because they will make it difficult to account for gravity. This is what my code looks like right now:

function module.Stepped(ball)
	local prevpos
	local stepped
	local bouncing = false
	local canbounce = true
	local bouncefactor = 0.5
	local i = 0
	
	local justbounced = false
	local hitpeak = false
	local peak = 20
	local maxbounce = 3
	local bounces = 0
	stepped = run.RenderStepped:Connect(function(dt)
		if not bouncing then
			i +=dt 
		elseif bouncing then
			i-= dt
		end
		if ball.Parent.Parent == nil then stepped:Disconnect() end
		if prevpos and ball:GetAttribute("LerpID") == nil and not bouncing then -- checks if the ball hasn't been hit yet
		
			ball:SetAttribute("Velocity", Vector3.new(0,  -workspace.Gravity/500 ,0) )
			ball.Position += ball:GetAttribute("Velocity") * i
		elseif not prevpos then
		
			ball:SetAttribute("Velocity", Vector3.new(0,  -workspace.Gravity/500 ,0) )
			ball.Position += ball:GetAttribute("Velocity") * i
		end
		local rayparams = RaycastParams.new()
		rayparams.CollisionGroup = "ServerBall"
		rayparams.IgnoreWater = true
		local groundray = workspace:Raycast(ball.Position, Vector3.yAxis * -1, rayparams)
		
		if groundray then
			if groundray.Instance and (groundray.Instance.Name == "Court" or groundray.Instance.Name == "Baseplate") and justbounced == false and canbounce == true and bounces < maxbounce then
				canbounce = false
					bouncing  = true
					justbounced = true
					bounces += 1
					ball.Anchored = true
					print("bounce")
					ball:SetAttribute("Velocity", ball:GetAttribute("Velocity") * Vector3.new(1,-1,1) * bouncefactor)
			
					justbounced = false
					task.spawn(function()
					task.wait(0.3)
					canbounce = true
					end)
				
			elseif bounces >= maxbounce then
				bouncing = false
				
			end
		end
		if bouncing == true and justbounced == false then
			if math.abs(ball.Position.Y - peak) < 2 and ball:GetAttribute("Velocity").Y > 0 then
				
				hitpeak = true
				ball:SetAttribute("Velocity", Vector3.new(0, -workspace.Gravity/500,0))
				bouncing = false
				peak = peak/1.7
				i=0
				task.wait(0.2)
				hitpeak = false
			else
				
			local gravity = Vector3.new(0, workspace.Gravity, 0)
			
				print(ball:GetAttribute("Velocity"))
			ball.Position += (ball:GetAttribute("Velocity") ) * i 
			end
		end
		 prevpos = ball.Position
	end)
end

There is no x and z axis implementation yet. Help is appreciated; thank you!

1 Like

bump bump bu,mp bump bump bump

1 Like

I forgot to mention but it is only for the bounce of the ball when it hits the ground

1 Like

Are you using a gravity simulation? Give the ball a velocity, ballVelocity (Vector3) and a gravity constant gravity (number).

local RS = game:GetService("RunService")

local ball = path.to.ball

local gravity = 10
local ballVelocity = Vector3.zero

RS.RenderStepped:Connect(function(dt)
    ballVelocity = Vector3.new(ballVelocity.X, ballVelocity.Y - (gravity * dt), ballVelocity.Z)
    ball.Position += ballVelocity * dt
end)

Are you using a system like this?

1 Like

yes I’m using a similar system; here is what I referenced :homework and exercises - Simple mathematical model for a bouncing ball - Physics Stack Exchange

1 Like

also I am already using a gravity constant, which is workspace.Gravity/500. The vertical bouncing works good, but I am worried about horizontal bouncing

1 Like

bump bump bump bump bump need help

1 Like

bump again… if nobodys gonna help i guess ill have to use bezier curves

1 Like

height = vt + 1/2at^2 where v is initial velocity, t is time, and a is gravity

assuming no air resistance, the position of the ball is a function of time given by

Vector3.new(
  xVelocity * time, 
  upwardInitialVelocity * time + gravity * (time^2) / 2,
  zVelocity * time
)

then when you bounce just reset time and decrease the initial upward velocity

1 Like

How would I calculate the upwards initial velocity?

1 Like

you dont calculate it

its just how much you push the ball upwards at first

after each bounce you should divide it by like 1.3 or something

so would it be (nextpos-startpos)/time?
Like how would you know how much you push the ball upwards at first?

1 Like

the first one is a random number like 10

then when you bounce you divide by how much energy you want to lose

in the real world its all different for different surfaces/materials so its whatever you want to choose to make it look realistic

So it isnt realy initial velocity then? Just a bouncefactor?

1 Like

also when it search it up it says the equation for initial velocity is u = v-at

1 Like

Alright I figured out the solution. Reference this sprite sheet for projectile motion:Horizontal and Vertical Velocity of a Projectile
as you can see, the x and z velocities never change, but the Y velocity accelerates due to gravity. So just accelerate the y axis velocity and keep the x and z axis velocities constant. Tweak the speed to your liking, but it works.

1 Like

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