2D Physics in Game Development
Author |
Message |
Nathan4102
|
Posted: 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:
Turing: | var x := 2.0;
for i : 1..1000
for ii: 1..1000
for iii : 1..1000
x := (x * 2) / 2
end for
end for
put(i);
end for |
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! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: 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:
Turing: |
setscreen("offscreenonly")
type characterData :
record
x, y, vx, vy : real
drawx, drawy : int
end record
fcn newCharacter (initx, inity : real) : characterData
var newc : characterData
newc.x := initx
newc.y := inity
newc.vx := 0
newc.vy := 0
result newc
end newCharacter
var player : characterData := newCharacter (100, 100)
var key : array char of boolean
loop
Input.KeyDown(key )
if key (KEY_RIGHT_ARROW) then
player.vx + = 0. 1
end if
if key (KEY_LEFT_ARROW) then
player.vx - = 0. 1
end if
if key (KEY_UP_ARROW) then
player.vy + = 0. 1
end if
if key (KEY_DOWN_ARROW) then
player.vy - = 0. 1
end if
player.x + = player.vx
player.y + = player.vy
% This is essentially friction
player.vx * = 0. 98
player.vy * = 0. 98
player.x := min(max(player.x, 0), maxx)
player.y := min(max(player.y, 0), maxy)
player.drawx := round(player.x )
player.drawy := round(player.y )
Draw.FillOval(player.drawx, player.drawy, 10, 10, 12)
View.Update
delay(5)
cls
end loop
|
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:
Turing: |
setscreen("offscreenonly")
% Add a gravity constant
const gravity := - 0. 1
type characterData :
record
x, y, vx, vy : real
drawx, drawy : int
end record
fcn newCharacter (initx, inity : real) : characterData
var newc : characterData
newc.x := initx
newc.y := inity
newc.vx := 0
newc.vy := 0
result newc
end newCharacter
var player : characterData := newCharacter (100, 0)
var key : array char of boolean
loop
Input.KeyDown(key )
if key (KEY_RIGHT_ARROW) then
player.vx + = 0. 1
end if
if key (KEY_LEFT_ARROW) then
player.vx - = 0. 1
end if
%Essentially this is the only input change
if key (KEY_UP_ARROW) and player.y <= 0 then
player.vy := 7
end if
player.x + = player.vx
player.y + = player.vy
player.vx * = 0. 98
% Add in gravity
player.vy + = gravity
player.x := min(max(player.x, 0), maxx)
player.y := min(max(player.y, 0), maxy)
player.drawx := round(player.x )
player.drawy := round(player.y )
Draw.FillOval(player.drawx, player.drawy, 10, 10, 12)
View.Update
delay(5)
cls
end loop
|
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 |
|
|
|
|
|
Insectoid
|
Posted: 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. |
|
|
|
|
|
Raknarg
|
Posted: 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? |
|
|
|
|
|
Nathan4102
|
Posted: 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. |
|
|
|
|
|
Raknarg
|
Posted: 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. |
|
|
|
|
|
Nathan4102
|
Posted: 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. |
|
|
|
|
|
Raknarg
|
Posted: 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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Nathan4102
|
Posted: 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. |
|
|
|
|
|
Raknarg
|
Posted: 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 |
|
|
|
|
|
|
|