5 posts
• Page **1** of **1**

What would be the best way to have hills in a top-down minigolf game using physics? I was thinking about an invisible sprite that just constantly applies a force in a certain direction to anything overlapping it. Is there some other way that would make more sense?

Follow my CZero development on twitter https://twitter.com/CaptainZeroGame

**Posts:**172**Reputation:**8,451

B

65
S

17
G

5
To add onto your idea, I would use some maths so that the force on the ball will be at a maximum 1/2 way up the slope with no force on top and no force at the bottom of the slope (simulating a varying gradient). Something like:

Force = ForceFactorVariable * sin( 180 * distance(ball.x, ball.y, hill.x, hill.y) / (hill.width * 0.5) ) **

ForceFactorVariable could be the mass of the ball x some other factor to get it to look right or to allow you to change the height of the hill. I would also add a check to correct Force to zero if Force < 0 , just in case the combination of collision polygon and distance measurement cause a negative value to be returned (that could happen in a corner of a square hill sprite, for example).

Hope this makes sense....

**edited for some bad public maths!!

Force = ForceFactorVariable * sin( 180 * distance(ball.x, ball.y, hill.x, hill.y) / (hill.width * 0.5) ) **

ForceFactorVariable could be the mass of the ball x some other factor to get it to look right or to allow you to change the height of the hill. I would also add a check to correct Force to zero if Force < 0 , just in case the combination of collision polygon and distance measurement cause a negative value to be returned (that could happen in a corner of a square hill sprite, for example).

Hope this makes sense....

**edited for some bad public maths!!

Last edited by Colludium on Mon Aug 18, 2014 7:04 pm, edited 1 time in total.

A big fan of JavaScript.

**Posts:**2,285**Reputation:**47,554

B

76
S

20
G

76
Colludium wrote:Force = ForceFactorVariable * sin( 180 * hill.width * 0.5 / distance(ball.x, ball.y, hill.x, hill.y) )

You're in the right track, but there are a few caveats:

- This presumes the force gets stronger, linearly, the closer the ball is. What a weird hill! This is also likely to send the ball into the stratosphere if it crosses the middle (which would be even weirder if the hill had a tapered top).
- You need to take into account the steepness of the hill.

Personally, I'd just spread a bunch of invisible "force-applying" objects on the screen, creating a sort of "force map".

**Posts:**532**Reputation:**6,903

B

36
S

8
G

8
@Fimbul,

Thanks for point out the poor logic and bad public maths on my part - the equation elements are the wrong way around!! That's what happens when you use your phone (poor excuse) for writing equations.

I've edited the original post and put the numerator and denominator the correct way around.

Edit: The idea with using sin is because sin(0) and sin(180) return zero - which should correspond to the top and bottom of the hill, whilst sin(90) which is the middle of the hill slope will return a 1 (max force factor value).

Thanks for point out the poor logic and bad public maths on my part - the equation elements are the wrong way around!! That's what happens when you use your phone (poor excuse) for writing equations.

I've edited the original post and put the numerator and denominator the correct way around.

Edit: The idea with using sin is because sin(0) and sin(180) return zero - which should correspond to the top and bottom of the hill, whilst sin(90) which is the middle of the hill slope will return a 1 (max force factor value).

A big fan of JavaScript.

**Posts:**2,285**Reputation:**47,554

B

76
S

20
G

76
5 posts
• Page **1** of **1**

## Who is online |

Users browsing this forum: No registered users and 4 guests |