Jump to content

Help with physics


WiLD11
 Share

Recommended Posts

Hello, I'm trying to make a clone of the game Plinko in Phaser.

(https://www.khanacademy.org/computer-programming/somewhat-improved-plinko/5438471890141184)

I want to be able to tell where the ball should fall using real physics.

help.pnh.png

If anyone can help with some code or an idea that would be great :) If you need any more details please let me know! I would really want to know how you guys will approach this one.

 

 

Link to comment
Share on other sites

Should be pretty simple. Set the appropriate gravity setting to control the fall of the player circle, but set the obstacles to immovable. Use circular physics bodies on all the circles. Use collision checks on update loop (group vs sprite, the obstacles are all in one group). 

Yeah, I saw your post about using tweens with easing functions for the bounce - you don't want to do that - this is a physics game, use physics. Instead, control the bounce of the player circle with the bounce property on the player circle physics body. 

Link to comment
Share on other sites

7 minutes ago, feudalwars said:

Should be pretty simple. Set the appropriate gravity setting to control the fall of the player circle, but set the obstacles to immovable. Use circular physics bodies on all the circles. Use collision checks on update loop (group vs sprite, the obstacles are all in one group). 

Yeah, I saw your post about using tweens with easing functions for the bounce - you don't want to do that - this is a physics game, use physics. Instead, control the bounce of the player circle with the bounce property on the player circle physics body. 

 

Makes sense, had all of those things in place except for the appropriate gravity setting. That's where I seem to be stuck at, could you perhaps help me further on that?

Link to comment
Share on other sites

When you run your collide function, Phaser will automatically separate game objects that collide. The default separation might not be what you want. To control the separation (i.e. the bounce) every sprite that has a physics body has a bounce property. This property is a multiplier. So for example, if you have a bounce of 1 and the physics body is moving at 50 pixels on both the x and y axis, and collides, it will bounce in the appropriate direction (determined by the collision function) at an x/y velocity of 50. If the bounce is set to .5, it will bounce in the appropriate direction at half the x/y velocity (25). 

You can get or set the x/y velocity of any physics-enabled sprite with sprite.body.velocity.x/y. You can get or set the bounce property with sprite.body.bounce.x/y. 

Link to comment
Share on other sites

My bad, your question was about the gravity, not bounce. 

http://phaser.io/docs/2.6.2/Phaser.Physics.Arcade.html#gravity

Every physics system has a gravity setting. It works the same way it does in the real world. The default (0/0) is deep space: no gravity, and your game objects will remain in place, unless something moves them. 

But if you're making a platformer, and you set the x gravity to 1, now there will be a constant force applied so that sprites so that they will constantly be sucked down until they bang into something (just like earth gravity).

 

 

Link to comment
Share on other sites

1 minute ago, feudalwars said:

My bad, your question was about the gravity, not bounce. 

http://phaser.io/docs/2.6.2/Phaser.Physics.Arcade.html#gravity

Every physics system has a gravity setting. It works the same way it does in the real world. The default (0/0) is deep space: no gravity, and your game objects will remain in place, unless something moves them. 

But if you're making a platformer, and you set the x gravity to 1, now there will be a constant force applied so that sprites so that they will constantly be sucked down until they bang into something (just like earth gravity).

 

 

 

I mean, that's all very clear to me but the problem is how I'm going to change the gravity. How will I know when to change/adjust it based on where I want it to land? 

Link to comment
Share on other sites

1 minute ago, WiLD11 said:

I mean, that's all very clear to me but the problem is how I'm going to change the gravity. How will I know when to change/adjust it based on where I want it to land? 

It's not a setting you chance periodically or mid-game. It's generally a constant, like in the real world. When you are on earth the gravity doesn't suddenly change :). 

Simply try a setting, see how the balls fall and collide against obstacles, and continue to adjust if you don't like it. 

Link to comment
Share on other sites

1 minute ago, feudalwars said:

It's not a setting you chance periodically or mid-game. It's generally a constant, like in the real world. When you are on earth the gravity doesn't suddenly change :). 

Simply try a setting, see how the balls fall and collide against obstacles, and continue to adjust if you don't like it. 

 

I don't think you fully understood, so I will try and explain further :)

I want to be able for example if I place a ball I would want to run a function that has a parameter which tells it where I want it to land, no matter where it is placed and it should work 100% of the time. 

Link to comment
Share on other sites

If you want to control exactly where the ball lands, in that case, physics isn't the answer as it's not a 100% deterministic system. You're better off using tweens... but I really don't think you want or need to go that route. 

I just played the game you are trying to clone. Why would you need to know ahead of time which slot a ball will fall into? Seems to me you can just increase the player score appropriately after the ball makes it into a slot. There's no need to know which slot it will go into before the ball reaches the destination. What am I missing?

Link to comment
Share on other sites

Just now, feudalwars said:

If you want to control exactly where the ball lands, in that case, physics isn't the answer as it's not a 100% deterministic system. You're better off using tweens... but I really don't think you want or need to go that route. 

I just played the game you are trying to clone. Why would you need to know ahead of time which slot a ball will fall into? Seems to me you can just increase the player score appropriately after the ball makes it into a slot. There's no need to know which slot it will go into before the ball reaches the destination. What am I missing?

 

I was assigned to make it into a casino game and one of their requirements was to make it fall where they want it to. I tried tweens but they said the tween looked too unrealistic and the paths were not diverse enough so I came here for help. Any other advice?

Link to comment
Share on other sites

12 minutes ago, WiLD11 said:

I was assigned to make it into a casino game and one of their requirements was to make it fall where they want it to. I tried tweens but they said the tween looked too unrealistic and the paths were not diverse enough so I came here for help. Any other advice?

Yeah, any physics system built by man is deterministic - the problem is that you don't have the CPU power in the browser to compute every possible outcome, hence why I refer to is as non-deterministic for all ostensive purposes.

Here's something you can do to make physics deterministic:

There is a fixed number of places the ball can drop from the top. Let's say, we divide the x axis length into segments of 10, so that you can only drop a ball every 10 pixels. To the player, this will mean the ball snaps to coordinates as he moves the ball across the top (this can be accomplished by using Phaser.Math.snapTo on the active pointer coordinates). For each of those coordinates, you drop a ball before the game starts and record the results. So if the width of the game is 200 pixels, you drop 20 balls (1 every 10 pixels, and record the results). That way, you know the outcome of where each ball will land based on where it is dropped from, before the game even starts. 

If you really want to go crazy, you can not have the ball drop from every 10, but instead have it drop from every pixel (200 balls for 200 pixels). That way, you won't have to limit the player to drop the ball from only a certain number of coordinates. The downside is that you'll need to do a lot of performance-heavy calculations at the start of the game .. but really, Phaser should be able to handle 200 balls in a small period of time (< 5 seconds). You could also run the simulation once and just keep the recording, so you don't need to re-calculate where the balls will drop each game. 

Link to comment
Share on other sites

P.S. You do have to think about floating points, and anything that can make your system non-deterministic. In theory, you should be able to calculate with a degree of 100% accuracy where a ball will land if you know the start x/y, and the gravity. But who knows? there could be some floating points screwing stuff up. So, best to test your results a lot to make sure your system is 100% deterministic. 

Link to comment
Share on other sites

12 minutes ago, feudalwars said:

Yeah, any physics system built by main is deterministic - the problem is that you don't have the CPU power in the browser to compute every possible outcome, hence why I refer to is as non-deterministic for all ostensive purposes.

Here's something you can do to make physics deterministic:

There is a fixed number of places the ball can drop from the top. Let's say, we divide the x axis length into segments of 10, so that you can only drop a ball every 10 pixels. To the player, this will mean the ball snaps to coordinates as he moves the ball across the top (this can be accomplished by using Phaser.Math.snapTo on the active pointer coordinates). For each of those coordinates, you drop a ball before the game starts and record the results. So if the width of the game is 200 pixels, you drop 20 balls (1 every 10 pixels, and record the results). That way, you know the outcome of where each ball will land based on where it is dropped from, before the game even starts. 

If you really want to go crazy, you can not have the ball drop from every 10, but instead have it drop from every pixel (200 balls for 200 pixels). That way, you won't have to limit the player to drop the ball from only a certain number of coordinates. The downside is that you'll need to do a lot of performance-heavy calculations at the start of the game .. but really, Phaser should be able to handle 200 balls in a small period of time (< 5 seconds). You could also run the simulation once and just keep the recording, so you don't need to re-calculate where the balls will drop each game. 

 

Great idea! But how would I add probability to that? How can I avoid it going to the jackpot if I drop it in the middle?

Link to comment
Share on other sites

To alter the results, you need to alter the parameters: the gravity setting and the position/number of obstacles. 

Run simulations with different x/y positions of the obstacles or different gravity settings, until you find the results you want (i.e. there is a .01 chance of hitting the jackpot). That's how I would do it.  

But if you are using physics, there's no way to alter the results without altering those paramaters, unless you wanted to cheat and for example, apply a gravity force if it gets to close to the jackpot :). 

Link to comment
Share on other sites

1 minute ago, feudalwars said:

To alter the results, you need to alter the parameters: the gravity setting and the position/number of obstacles. 

Run simulations with different x/y positions of the obstacles or different gravity settings, until you find the results you want (i.e. there is a .01 chance of hitting the jackpot). That's how I would do it.  

But if you are using physics, there's no way to alter the results without altering those paramaters, unless you wanted to cheat and for example, apply a gravity force if it gets to close to the jackpot :). 

 

I see, thank you so much! I will try it out and post an update here once I got something.

Thanks again :) 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...