# Help with barriers for balls in mini golf game?

#1

I’m trying to make a mini-golf game and I need a reliable way to have the ball bounce off walls. With my current system, (which is having my walls made of lots of little squares that detect when the ball is near, and then reverses the momentum,) the ball goes through the wall when it is going even remotely fast. Any help would be appreciated and screenshots would also be helpful. Thanks.

#2

I’m not sure how to, but people like @CreationsOfaNoob and @ThinBuffalo May be able to help.

#3

How are you making the ball move? I think the best way to do this in Hopscotch is to set the angle based on the distance dragged and use move forward so it doesn’t jump 8 pixels per frame or something like that. And also use square collisions (absolute value(wallX-ballX)<60)

Although with angle you willl have to know sin and cos, which like Aariv said, ask @CreationsOfaNoob @Petrichor or @ThinBuffalo, they would know more than me

#4

The collision detection for a ball bouncing off a wall is deceptively difficult to execute without flaws & without causing lag in Hopscotch.

I published some thoughts on the subject previously on my general coding topic. I’ll find that and reformat it for this topic when I have time today. For right now, I’ll point you to my BreakOut project where I’ve done this. The code for mini-golf might be easier if the ball can only hit a given barrier on one side. I’ll have to think about that.

Have a look through the code here:

Here’s another, simpler, example to look at (that also takes the diameter of the “ball” into account:

#5

I mean, that’s kinda helpful, but I don’t really understand all that maths and an explanation of what it is actually doing might be helpful. (I looked at the 2nd one.)

#6

I’m pretty good on sin and cos, as in, I can make a circle, but I don’t really know any other ways to use it. Currently I am doing ‘Set Position X selfXpos+Xvelocity Y selfYpos+Yvelocity to make the ball move.

#7

BTW, with my golf course, you are looking down at it from the top, so I don’t need gravity. Also, I currently have a system that sets the momentum to where you drag from, and then let go. You touch the ball, drag back, and when you release, it ‘launches’. If I am going to need to have a whole new system, then can I have a new way to do that too? Thanks.

#8

I think that is the issue, as the velocities make the ball set its position too far

#9

I’m going to give you another project to look at:

This one uses a collision technique that’s much simpler than I used in the above projects. It’s limited to horizontal and vertical barriers & glitches in some instances when the ball is hit too hard, but overall works really well so I think it’s a good compromise between complexity and accuracy.

Each line is one object that knows which side the ball can hit it on (based on a “direction” variable). Collisions are determined by attempting* to calculate if the ball will go past the horizontal/vertical location of the wall during the next movement step (remember, displayed movement on a digital device is a series of discrete steps that our brains just process as smooth analog movement), while at the same time is in between the end points of the wall.

Note: I said attempting above because if you look at the formula for the ball’s step size verses the collision calculation, there’s a discrepancy but it apparently doesn’t affect the results too much.

#10

I mean, I looked at the project, but it looks really complicated to me, and I only looked at the ‘Wall’ object. Surely there is some kind of simple way? Or is it all just too hard…

#11

Sorry if I’m being a little demanding… I just want some simple fix. Sigh

#12

TheBest1Ever’s approach is about as simple as it gets. Let’s look at the case of a vertical barrier that can be hit from the left…

I’ll change the variable names to clarify, where:

Radius = Ball Diameter / 2
Xvelocity = distance ball moves horizontally per frame
X/Y = current position of moving ball
X1/Y1 = upper end point of the barrier
X2/Y2 = lower end point of the barrier

If {the position of the ball will be past the barrier} And {the ball is between the barrier end points} Then

translates to

If (X + Xvelocity + Radius > X1) And ((Y<Y1) and (Y>Y2) Then
{collision occurs so reverse the X direction}

which is

Set Xvelocity = Xvelocity * -1

Extend this for barriers that can be hit on the right (vertical), bottom (horizontal), and top (horizontal)…

#13

Okay… I think I get it better now. I’ll try and implement it into my game. Thanks again!

#14

Hmmm… I implemented the code in exactly as you said, and then shot it at the wall, but it didn’t work. I even made it clone specific, so only one wall would work, but a whole lotta nothing happens. This is kind of annoying…

#15

Actually, I put in ‘Speed’ instead of ‘X velocity’. I implemented the set angle and move forward system, but I don’t know how to translate that into a ‘X velocity’ number.

#16

That can be determined with trigonometry.

Given a right triangle (90 deg)
Sin(angle) = O/H
Cos(angle) = A/H

Where,
Speed = hypotenuse
Y velocity = opposite

So solving,
X velocity = Cos(rotation)/Speed
Y velocity = Sin(rotation)/Speed

#17

Awesome! But just wondering, I have a move forward block running constantly, right? How much should the ball be moving forward by? Right now I think I have ‘Repeat Forever Move Forward 10’. Is there some correct number?

#18

Just make it a really big number.

The amount of pixels the object actually moves forward per frame is it’s Speed/40 (that’s just a native Hopscotch definition that you can work out, but isn’t actually explained anywhere)

So since we’re talking about the Speed Trait and not a variable named Speed (which is what I was thinking earlier), the equations for X/Y velocity are actually

X velocity = Cos(rotation)/(Speed/40)
Y velocity = Sin(rotation)/(Speed/40)