Computer Science Canada 2D Physics in Game Development |
Author: | Nathan4102 [ Thu Feb 27, 2014 10:40 pm ] | ||
Post subject: | 2D Physics in Game Development | ||
Hey guys, I'm starting work on my ICS3UI summative, basically I'm planning on a 2D physics related game, I'd rather not give the specifics. It'll include vertical momentum, gravity changes (Direction and magnitude), inertia, you name it. Basically I need it looking as realistic as possible, which means not jumping at a constant 2m/s, or having your momentum shift from 2m/s up to 2m/s down in a tick when gravity changes. This doesn't seem like too much, except I've never taken a physics course. I don't know how an instantaneous change of direction of gravity would affect a person in flight, I don't know how to calculate the path of a jumping person, I don't know any of that stuff. How do you guys suggest I learn all this stuff? Like sure I could look up a whole bunch of physics stuff and learn how to calculate this stuff using fancy formulas, but I feel like actually implementing it in a 2D game isn't that simple. I've looked up a bit on 2D game development, and 2D physics, but none of the stuff I've found has been too useful. Any suggestions? Also, how should I control the rate at which the game progresses? Like in the past, I've had my main loop which calls on calculations and drawing functions etc, then waits x miliseconds, then does the loop again. This is alright except it'll run faster on a good computer, and slower on a slow old computer. Ideally, I'd like the computers processing power to determine the FPS, not the rate the game actually progresses. How would I go about doing this? Like take this turing program for example:
This program will print numbers at different speeds on different computers. Ideally if the numbers were frames, it should skip numbers on slow computers, and print at a constant speed on fast computers(Or maybe even decimals). How would I go about doing this in a program? Thanks! |
Author: | Raknarg [ Thu Feb 27, 2014 11:50 pm ] | ||||
Post subject: | RE:2D Physics in Game Development | ||||
For the physics part, don't make it too complicated. Here's a couple examples. Here's a ball with "inertia". The actual momentum can be calculated if you have a variable to represent mass. My hope is that by now you know how to use classes, which is a much better method of doing this. Basically you have variables to represent your position and some to represent your velocity. To change direction, what you do is add or subtract to your velocity. If you're going right 5 m/s and you want to go left, you slow down by 1m/s every second or whatever. This will look more realistic. You'll slow down, then start going left, like in this example:
Pretty simple. If you want to do other physics with them, as long as you know the size and mass of them you should be able to do it. This is the basics. You can also do realistic jumping with gravity with a few small tweaks:
As for how to conserve speed across computers, Insectoid wrote a nice tutorial in Turing on that very subject here. You can also use Time.DelaySinceLast which will help but it's not a perfect solution |
Author: | Insectoid [ Fri Feb 28, 2014 9:22 am ] |
Post subject: | RE:2D Physics in Game Development |
Having objects interact with each other with realistic physics is actually pretty difficult. Making (slow) things bounce off each other realistically is a simple application of F = ma (force = mass * acceleration). Objects moving at high speeds is a bit harder, because they can collide between frames. For example, in frame 1, object A is on a collision course with object B. In frame 2, object A is past object B. the collision happened between frames. You need to account for that, which requires some extra math (especially if you use my delay-free animation method that raknarg mentioned). Then of course you get into rotation, which is even more difficult, because you start dealing with centre of mass, rotational inertia, etc. A half-decent physics engine that can handle all of this is several hundred lines by itself, all of which is pretty hard to understand. I'm not trying to discourage you, just to let you know what you're getting yourself into so you can evaluate if you have the time to do all of this. |
Author: | Raknarg [ Fri Feb 28, 2014 10:12 am ] |
Post subject: | RE:2D Physics in Game Development |
Given this, if you want to pursue this you should look up some really nice tutorials and make sure you understand the physics you are trying to do, otherwise your code will: a) be an abomination b) make you rip your hair out SO by realistic are you talking about realistic collisions as well? |
Author: | Nathan4102 [ Fri Feb 28, 2014 12:02 pm ] |
Post subject: | RE:2D Physics in Game Development |
Thanks Raknarg for the examples, they should get me going in the right direction atleast. It looks like it shouldn't be TOO complicated to do what I want to do. And Insectoid, I won't be dealing with any rotations or super fast moving objects, so these collisions shouldn't be too complicated I believe. It would be cool to have the character or object bounce off an obstacle in a realistic manor, but definetly not needed, and I won't add any rotational stuff until I have the basics down first. @Raknarg again, realistic collisions aren't required, I might opt into adding them in the future, but first I'd just like to get a 'beta' version with all the basic physics stuff down. |
Author: | Raknarg [ Fri Feb 28, 2014 12:43 pm ] |
Post subject: | RE:2D Physics in Game Development |
Good idea. If you decide that you want to use more realistic physics, I suggest looking deeper into the math and utilizing classes to represent vectors instead. |
Author: | Nathan4102 [ Fri Feb 28, 2014 4:53 pm ] |
Post subject: | RE:2D Physics in Game Development |
I should probably learn how to properly use classes first I'm experienced with instantiating classes like the Scanner class and DecimalFormat class in java, but thats honestly about all I can do with classes. |
Author: | Raknarg [ Fri Feb 28, 2014 5:40 pm ] |
Post subject: | RE:2D Physics in Game Development |
Then this might be a good time to learn. You'll have to learn at some point in fact it might be better to learn how to use classes with Java instead, its object support is much nicer. Of course you can still do it in Turing. In any case, once you're comfortable with them you probably won't want to go back. Much like learning arrays, you realize how much simpler and nicer it makes your code and how much easier it is to maintain and to make changes |
Author: | Nathan4102 [ Fri Feb 28, 2014 5:43 pm ] |
Post subject: | RE:2D Physics in Game Development |
Ah ok, maybe monday in ICS ill work on some tuts. And Im done wirh turing, I just made the example in my OP in turing because its easy to whip something together quick. This game will be done in Java. |
Author: | Raknarg [ Fri Feb 28, 2014 6:19 pm ] |
Post subject: | RE:2D Physics in Game Development |
Then I suggest going to look at www.processing.org |