123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|375|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> On Topic -> bouncing a ball

Thu, 07 Jul 2011, 22:58
jedimastersterli
Ok I want to start physical simulation programing, and what better way to start than throw a ball with your mouse and watch is bounce arround the screen. I know some basic C and C++ programing (self taught), i think i'll need to learn java to get the graphics to work.

Has anyone ever done something like this and could tell me where to start.
Fri, 08 Jul 2011, 11:28
shroom_monk
The thing with physical simulations is that the more full you want your simulation to be, the more complicated it's going to be to make. For instance, here you just want to throw a ball about the screen, so you don't really need to worry about things like rotation. All you need to cover would be the velocity vector of the ball and the accelerations applied to it by the mouse and / or gravity, along with the bounces off the walls. Out of interest, how much of the maths and stuff behind this do you already know?

As for the graphics, you can do it in Java, although if you do want to do it in C++, you could try a graphics library such as SFML.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Fri, 08 Jul 2011, 17:21
steve_ancell
Thanks for posting that link shroom, I was thinking about having another go at learning C++, so SFML might come in handy.
Sat, 09 Jul 2011, 03:53
shroom_monk
Someone else on here (was it Phoenix? I forget) recommended it to me a while ago, and I've been using it since then. I like it.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Sat, 09 Jul 2011, 10:21
jedimastersterli
The physics is easy, just pick an acceleration at feet per second squared that looks natural, the final velocity of the mouse is the initial velocity of the ball, and since the walls are square reverse the i or j vector whenever it hit's a wall, and i might want to do some craziness like it looses 20% speed when it hits something so that after a while it stops bouncing. The problem is how to tell all of that to a computer.
My biggest question is how to get real time results. Could i just have "structure ball" with variables i, j, x and y which calculates change in all for each visual cycle then prints to screen and repeats, therefore being incremental rather than constant. I think i just answered my question. By the way thanks for the hint, i think i'll take a look into SFML, it would be easier than learning a whole new language especialy when i'm still having problems in C++.
Sat, 09 Jul 2011, 12:14
shroom_monk
Yeah, you'd do best to make the ball it's own object, and have it handle all of its physics calculations. If you go with SFML, you can ask it to tell you how many seconds have passed since the last time you updated everything, and use that value to keep everything smooth, whatever your framerate is.

So, for instance, if your velocity was 10 feet/second and the elapsed time since the last frame was 0.05 seconds, you could easily multiply those values together to get the distance (in feet) travelled.

'Telling it to the computer' shouldn't be too hard either - I find it easiest to write code one small step at a time, and test that before moving on, rather than bung it all in and get non-functional code. One way of doing it might be to first make your ball class, then make it start with some initial velocity to make sure that the calculations run properly, and then, when all that is working, add the mouse throwing bit. Of course, it's up to you how you do it, but if you're having trouble conceiving the entire solution in your head, it might be worth breaking it down into smaller tasks first.

Hope that's all some help.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!