Hey, so I’ve been trying to create a 2D lane visual that shows you the path the ball travelled after you threw the ball. So like, if the ball hooked a lot, the visual would basically be like a graph of the ball’s path.
I thought this would be fairly easy to do, and I think it is, but I’ve been struggling trying to figure out the right logic. This is what I have currently:
for i,v in pairs(ball_track) do
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0,1,0,1)
frame.BackgroundColor3 = Color3.new(1,0,0)
frame.ZIndex = 3
local lane_x_max = currentOil.Core.Position.X + (currentOil.Core.Size.X / 2)
local lane_x_min = currentOil.Core.Position.X - (currentOil.Core.Size.X / 2)
local lane_y_max = currentOil.Core.Position.Z + (currentOil.Core.Size.Z / 2)
local lane_y_min = currentOil.Core.Position.Z - (currentOil.Core.Size.Z / 2)
local x = math.clamp(v.X, lane_x_min, lane_x_max) / lane_x_max
local y = 0
print(x)
frame.Position = UDim2.new(x,0,y,0)
frame.Name = "Track"
frame.Parent = bowling_frame.StrikeTrack.Lane
end
So basically, “ball_track” is a table containing all the positions the ball has travelled to going down the lane, and then it’ll loop through each position to render a dot to create the 2D path on the player’s screen.
So the general idea was to like, create a minimum and maximum value for the X and Z positions on the lane, and I could use those coordinates and then compare it with the ball’s position to get a value between 0-1, and then I could set each dot’s scale position to that and offset it to create a more realistic track.
Unfortunately, I was nowhere near close when I tested this. Any help is greatly appreciated! I can also provide you with more info if needed. Thanks for reading!
Ok I think your getting confused I’m on iOS rn though so really you what you can do,you did the Table saving right but you know in pathfinding where it shows the waypoints,copy the waypoints function and edit it to your needs
Reply to C_Sharper
Its a bit difficult to help as we are not able to test out any theories we may have because we don’t have details of what is in ball_track and currentOil.
I am guessing that ball track is a table containing points along the path the ball took and currentOil.Core is a part on the lane that has a specific slippy property.
Would it be possible to let us have some details about those items?
So yeah, ball_track is a table of all the positions that the ball has travelled to when it was rolling down the lane, we’ll use these to draw 2D points on a UI frame to show the track the ball travelled. (That’s the goal)
The Core part of currentOil is the size of the lane, essentially. I tried applying some logic to the position of the core part and comparing it with the ball’s position, but I haven’t had much success.
Look into the bezier curve, it’s the foundation for forming a smooth curve between three or more points on computer software. Lerp would be the best Roblox equivalent.
Thanks for the idea, although I believe there’s a simpler way to do this.
The idea I had would be to divide the ball’s current X position by the X position of the lane part + half of the lane size, so that’d be the maximum X. In my mind’s eye, the value returned would be from 0-1, and I could then use the value and set the Scaled position of each dot. Unfortunately, I’ve been getting weird results from this…
Is there any reason why y is equal to 0? You use math.clamp for the x. Also you’re generating lane_y_max and lane_y_min for no reason if y is supposed to be set to 0.
As @CodeJared said, I’m not totally sure what’s going on with the Y axis, but I think I can see the issue with the X axis:
Assuming your lanes aren’t rotated, lane_x_max and lane_x_min should be correctly calculated, and the math.clamp will correctly ensure that the position of the ball doesn’t stray outside the bounds of the lane.
However, when calculating the final offset, you’re not accounting for the offset of the lane from the world origin. As a result, you’re probably getting a lot of values close to 1, since it’s giving you the fraction of the distance from the world origin to the end of the lane, rather than from the start of the lane to the end of the lane. This is hard to explain, so here’s a picture that maybe helps:
local x = ( math.clamp(v.X, lane_x_min, lane_x_max) - lane_x_min ) / ( lane_x_max - lane_x_min )
As an aside, if your lane is perfectly centred on the X axis, this will result in a divide by 0 error. It’s easy enough to just avoid this case, but if you want to handle it then you’ll need to check if lane_x_max == lane_x_min, and if so then use your original formula.