OK, basically i have an assigment due in on the 6th of may, and i chose to make an avalanche game for it in C++, using microsoft visual studio 2010, with the SDL library i think.
(if you dont know this game, just google 'Avalanche Game' and youll find it, the game is basically the user jumping on blocks falling from the sky and trying to climb up as far as he can.)
I already have a few problems, since our lecturer is completely stupid he cannot teach to save his life, so everyone is kind of stuggling with C++ because of him.
In my game im having trouble making my sprite(bitmap sprite) have gravity, that works realistically and i have no clue how to make him jump when the user presses the up arrow. I know this seems silly that i dont know that but i really need some help with it, i spent days looking for guides on google and found nothing that could help me.
struct dude{
float x, y;//where you draw the bitmap
float vx, vy; //velocity
void update(){//a function which is called on every frame
if( I_am_in_air() ) vy += 0.1;//gravity
else vy = 0;
if( space_is_down() ) vy = -10;
x += vx;
y += vy;//movement
}
};
Note that my constants are random numbers. You'll probably want to change them.
I suggest you to separate the data acquisition (keys pressing), the physics update and the frame drawing. Another thing - the game being simple, you might get a lot of extra time - more than enough to draw 30 or 60 fps. Take care of it. In this light you don't consider relating frames with physics. You should have timers as reference, and that physics update process have to consider the time as a primary factor in the calculus.
I noticed that the Wiki linked at the SDL website is lacking in useful information, I found this though: http://www.libsdl.org/cgi/docwiki.cgi/SDL_Guide and it has been a life saver with some of the things I've tried to do.
EDIT: As userulluipeste said DO NOT RELATE PHYSICS TO FRAMES PER SECOND. This is a common mistake that people make to apply things like gravity at every iteration of the loop, an actual timer is a far better solution and not difficult to implement.
whoa tryed to post a long reply, but something made my internet die...
so im going to sum it up.
@packetpirate i feel sorry for you dude, the guy i have teaching us is at Kingston uni
and userulluipeste, is there any good guides that could teach me how to use the timers? because they seem like a better idea since i still cant figure out how to make my gravity, even with the help i already got, its just not working :/
makes me feel so dumb, that i cant even do gravity :(( i probably could on something like DevC++ but were supposed to use visual studio 2010 :/ i hate it, its confusing.
Sorry, kelso! I had no easy way for myself. I guess you'll have to start looking a bit over laws of classical mechanics. What interests you is figuring out the position in time of some object in a space with a gravity field. That means first a relation between distance and time - speed. Then relate speed with time, resulting acceleration. Acceleration itself is a relation between force of gravity and mass. Mass is constant (the weight of your fellow in the game), force is constant (or at least you may consider it so). The time is the one that varies and therefore - the speed. Again, for a given time and speed, you can calculate the distance between last time and current time. And this is only about the up/down movement, like when you press the command for bouncing which inflict an anti-gravitational force for one-time calculus. If you want left-right you'll have to compose/decompose the forces for each of the space's direction (2 for your game).
These things however, are the job of a physics engine, which would be itself just a part of a game engine complete solution. You may consider studying some open-source game engines.