gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > gravity/jumping physics -need heelp

#82889 - Dan2552 - Thu May 11, 2006 1:20 am

edit-
see post below


Last edited by Dan2552 on Thu May 11, 2006 1:51 am; edited 1 time in total

#82893 - Darkflame - Thu May 11, 2006 1:30 am

Well, your friend is obviously very wise to pick such an equation.
Clearly an inteligent person there.

Now..lets look at your code.
hmm..

#82895 - Darkflame - Thu May 11, 2006 1:37 am

Ok, my knowledge of this code type limits me, but

is frodo1_y an interger?

Because maybe its not reaching "100" exactly, and thus it goes though the floor.

So maybe, rather then detecting Y=100 as the floor, you should detect
(y<100 and y>95 )

(or make frodo an interger)

#82899 - Dan2552 - Thu May 11, 2006 1:51 am

update, sorry - lots of problems which i probably could've solved with a bit more thought

[ignore graphical scripts]
[ignore 2nd character]
[ignore bad scripts (unless they're to do with jumping/gravity)]

http://www.hingedshinobi.co.uk/axek/Divergence.rar

Code:

/*********************************************************************************
 * Includes
 ********************************************************************************/
// Basically, first, as on top of all HAM files, the most
// important include is the mygba.h, which contains all the
// macro definitions, and the HAMlib function prototypes.
#include <mygba.h>

#include "gfx/bg.pal.c"
#include "gfx/bg.raw.c"
#include "gfx/frodo.pal.c"

#include "gfx/LEFTfrodo.raw.c"
#include "gfx/RIGHTfrodo.raw.c"
#include "gfx/RIGHT1frodo.raw.c"
#include "gfx/RIGHT2frodo.raw.c"
#include "gfx/LEFT1frodo.raw.c"
#include "gfx/LEFT2frodo.raw.c"

#include "gfx/LEFTfrodo2.raw.c"
#include "gfx/RIGHTfrodo2.raw.c"
#include "gfx/RIGHT1frodo2.raw.c"
#include "gfx/RIGHT2frodo2.raw.c"
#include "gfx/LEFT1frodo2.raw.c"
#include "gfx/LEFT2frodo2.raw.c"




MULTIBOOT



/*********************************************************************************
 * Globals
 ********************************************************************************/
u8 g_NewFrame=TRUE;
u32 frames     =   0;
u32 frames2     =   0;
u8 frodo1;
u8 frodo1_x = 25;
u8 frodo1_y = 30;
u8 frodo1speed = 1;
u8 frodo2;
u8 frodo2_x = 198;
u8 frodo2_y = 30;
u8 frodo2speed = 1;
u8 dir_frodo1 = 0;
u8 leftright1 = 1;
u8 frameslr = 0;
u8 dir_frodo2 = 0;
u8 leftright2 = 1;
u8 frameslr2 = 0;
u8 speedfall = 3;
u8 speedfall2 = 2;
u8 lockup = 0;
u8 lockup2 = 0;

u8 jumping = 0;



/*
s = distance  (from ground, for instance)
u = starting speed
t = time
a = acceleration   (  -9.81 for gravity )
*/
u8 s =0;
u8 u =9;
u8 t =1;
u8 a = 9.81;


/*********************************************************************************
 * Prototypes
 ********************************************************************************/
void vblFunc(void);


/*********************************************************************************
 * Program entry point
 ********************************************************************************/
int main(void)
{
    ham_Init();

    // Will, once activated, trigger the specified interrupt,
    // and then call the function specified
    ham_StartIntHandler(INT_TYPE_VBL,         // The Interrupts ID you want to start.
                        (void *)&vblFunc);    // The adress of a function that should be called when the interrupt is fired
   ham_SetBgMode(4);
   ham_LoadBGPal((void*)bg_Palette,256);
    ham_LoadBitmap((void*)bg_Bitmap);
   ham_FlipBGBuffer();
   
    ham_LoadObjPal((void*)frodo_Palette, 256);
    frodo1 = ham_CreateObj((void*)RIGHTfrodo_Bitmap,OBJ_SIZE_32X32,
                             OBJ_MODE_NORMAL,1,0,0,0,0,0,0,frodo1_x,frodo1_y);
  //  ham_LoadObjPal((void*)frodo3_Palette, 256);
    frodo2 = ham_CreateObj((void*)LEFTfrodo2_Bitmap,OBJ_SIZE_32X32,
                             OBJ_MODE_NORMAL,1,0,0,0,0,0,0,frodo2_x,frodo2_y);
   
    // Loop
    while(TRUE)
    {
        // It's a new frame?
        if(g_NewFrame)
        {

if (frodo1_y == 100) {
    u= 0;
    t=0;
}
if (frodo1_x < 23) {
  u=0;
  t=1;
}


query_buttons();
update_plane_posfrodo1();
query_buttons2();
update_plane_posfrodo2();

            // Frames isn't new anymore
            g_NewFrame=FALSE;
        }
    }

    return 0;
}

/*********************************************************************************
 * vblFunc(void)
 *
 * This function is called whenever the GBA is about
 * to draw a new picture onto the screen.
 ********************************************************************************/
void vblFunc(void)
{
    // Call this (preferably during a VBL interrupt) to commit your
    // ham_obj information to the hardware. Only after you did this your
    // sprite changes will appear on screen.
    ham_CopyObjToOAM();
++frames;
++ frameslr;
++frames2;
++ frameslr2;
    // It's a new frame yet
    g_NewFrame=TRUE;
}


void query_buttons()
{
if(F_CTRLINPUT_UP_PRESSED)   {

if (frodo1_y == 100) {
 u = 10;
 t=1;
 frodo1_y = frodo1_y -1;
}
return;
}
   
    // LEFT
if(F_CTRLINPUT_LEFT_PRESSED)    {
 if (frodo1_x > 1) frodo1_x = frodo1_x - frodo1speed;
 dir_frodo1 = 1;
 if (frameslr > 20) {
 frameslr = 0;
 if (leftright1 == 1) {
 ham_UpdateObjGfx(frodo1,(void*)&LEFT2frodo_Bitmap);
 leftright1 = 2;
 goto skip2thing;
 }
 if (leftright1 == 2) {
 leftright1 = 1;
 ham_UpdateObjGfx(frodo1,(void*)&LEFT1frodo_Bitmap);
 goto skip2thing;
 }
 skip2thing:
 }
 return;
}
        // RIGHT
if(F_CTRLINPUT_RIGHT_PRESSED)   {
 if (frodo1_x < 237) frodo1_x = frodo1_x + frodo1speed;
 dir_frodo1 = 2;
 if (frameslr > 20) {
 frameslr = 0;
 if (leftright1 == 1) {
 ham_UpdateObjGfx(frodo1,(void*)&RIGHT2frodo_Bitmap);
 leftright1 = 2;
 goto skip1thing;
 }
 if (leftright1 == 2) {
 leftright1 = 1;
 ham_UpdateObjGfx(frodo1,(void*)&RIGHT1frodo_Bitmap);
 goto skip1thing;
 }
 skip1thing:
 }
 return;
}
   

if(F_CTRLINPUT_DOWN_PRESSED)   {

}

} //end of query_buttons



void update_plane_posfrodo1() {
   
if (frodo1_y != 100) {
frodo1_y = frodo1_y +  0.01 *  (   (u*t)+((1/2)*a*t*t)  );
t=t+1;
}
if (frodo1_x<23) {
frodo1_y = frodo1_y +  0.01 *  (   (u*t)+((1/2)*a*t*t)  );
t=t+1;
}
    ham_SetObjX(frodo1,frodo1_x);
    ham_SetObjY(frodo1,frodo1_y);

    if (frames > 40) {
        // Reset the frame counter
        frames = 0;
        // Update it
    if (dir_frodo1 == 1) {
ham_UpdateObjGfx(frodo1,(void*)&LEFTfrodo_Bitmap);
}
    if (dir_frodo1 == 2) {
ham_UpdateObjGfx(frodo1,(void*)&RIGHTfrodo_Bitmap);
}
}
    return;
} //end of update_plane_posfrodo1






void query_buttons2()
{

if(F_CTRLINPUT_START_PRESSED)   {

return;
}
   
    //left
    if(F_CTRLINPUT_B_PRESSED)    {
        if (frodo2_x > 1) frodo2_x = frodo2_x - frodo2speed;
        dir_frodo2 = 1;
        if (frameslr2 > 20) {
    frameslr2 = 0;
if (leftright2 == 1) {
     ham_UpdateObjGfx(frodo2,(void*)&LEFT2frodo2_Bitmap);
     leftright2 = 2;
    goto skip3thing;
}
if (leftright2 == 2) {
     leftright2 = 1;
     ham_UpdateObjGfx(frodo2,(void*)&LEFT1frodo2_Bitmap);
goto skip3thing;
 }
 skip3thing:
 }
        return;
    }
        // right
    if(F_CTRLINPUT_A_PRESSED)   { //A
        if (frodo2_x < 237) frodo2_x = frodo2_x + frodo2speed;
dir_frodo2 = 2;
        if (frameslr2 > 20) {
    frameslr2 = 0;
if (leftright2 == 1) {
     ham_UpdateObjGfx(frodo2,(void*)&RIGHT2frodo2_Bitmap);
     leftright2 = 2;
    goto skip4thing;
}
if (leftright2 == 2) {
     leftright2 = 1;
     ham_UpdateObjGfx(frodo2,(void*)&RIGHT1frodo2_Bitmap);
goto skip4thing;
 }
 skip4thing:
 }
        return;
    }
} //end of query_buttons

void update_plane_posfrodo2() {
    ham_SetObjX(frodo2,frodo2_x);
    ham_SetObjY(frodo2,frodo2_y);

    if (frames2 > 40) {
        // Reset the frame counter
        frames2 = 0;
        // Update it
    if (dir_frodo2 == 1) {
ham_UpdateObjGfx(frodo2,(void*)&LEFTfrodo2_Bitmap);
}
    if (dir_frodo2 == 2) {
ham_UpdateObjGfx(frodo2,(void*)&RIGHTfrodo2_Bitmap);
}
}
    return;
} //end of update_plane_posfrodo1
/* END OF FILE */

#82900 - Darkflame - Thu May 11, 2006 2:00 am

It should be fairly easy to get this working...if someone knew more knowledge then me about this :p

#82903 - tepples - Thu May 11, 2006 2:33 am

The way most 2D games seem to do platform physics is to use Euler integration. Do the following in each frame for each sprite:
  1. Add a constant to Y velocity. (Adjust this constant to reflect gravity and the scale of your game world.)
  2. Add X velocity to X displacement and add Y velocity to Y displacement.
  3. Perform collision tests and push the character away from walls, floors, and ceilings as appropriate.
  4. Draw the sprite at position (X displacement - X camera origin, Y displacement - Y camera origin).

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#82905 - Darkflame - Thu May 11, 2006 2:39 am

Euler is sooo outdated :-/

S=ut is easy and more accurate.
I could make this work easily in a code type I know...but I dont know c++ or gba dev

#82906 - tepples - Thu May 11, 2006 2:43 am

What is S=ut? Google doesn't help. And can it deal with things such as wind resistance (important if you're trying to do something like that level of Super Mario Bros. 2 (J) with the wind-assisted jumps)?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#82908 - Dan2552 - Thu May 11, 2006 2:52 am

tepples wrote:
What is S=ut?


*points at big mass of crap code above*


frodo1_y = frodo1_y + 0.01 * ( (u*t)+((1/2)*a*t*t) );


I think darkflame called it S=ut himself :p

Quote:

s = ut + (1/2)at? ( That is, s=(u*t)+((1/2)*a*t*t) )

s = distance (from ground, for instance)
u = starting speed
t = time
a = acceleration ( -9.81 for gravity )


I think the advantage in this method is the acceleration?

#82909 - Darkflame - Thu May 11, 2006 3:08 am

Yes,acceleration, and you avoid an if statement.

(and it also lets things fall down, and bounce easily later)

tepples wrote:
What is S=ut? Google doesn't help. And can it deal with things such as wind resistance (important if you're trying to do something like that level of Super Mario Bros. 2 (J) with the wind-assisted jumps)?


Sorry, my fault, here look at this;
http://www.physchem.co.za/Graphs/Equations.htm

I am a physics person, so I find this stuff pretty basic.
Most new computer games use it, as does Newton :p

s = ut + (1/2)at?

ut = how far it goes in a given time (speed x time)
+(1/2)at^2 = how the acceleration effect it over time.

a= - 9.81 for gravity, naturely.



---
It isnt to do with Wind...allthough...wouldnt be too hard to add (another day :p)

#82912 - poslundc - Thu May 11, 2006 3:34 am

Darkflame wrote:
Euler is sooo outdated :-/

S=ut is easy and more accurate.


If it's so easy then why can't you seem to get it working? ;-)

In all seriousness, the Euler method is better. It is very, very difficult to design a stateless system of composite forces that is at all scalable or generic. State-tracking is far more sensible, if your intent is to make a game, anyway.

Accuracy is not a problem in games, either. At least, if you use the value 9.81 m/s/s in 16.16 fixed-point format, then at 60 ticks per second your error will be 0.000122 m (or about 0.1 millimetres) per second.

If you're satisfied with the accuracy of 9.81 m/s/s as your gravitational constant but not satisfied with an error of 0.1 mm/s, then you probably shouldn't be designing games for computer systems.

Dan.

#82913 - Dan2552 - Thu May 11, 2006 3:43 am

poslundc wrote:
then you probably shouldn't be designing games for computer systems.


DF isn't designing games, atleast for GBA.

I just asked how to do good gravity and jumping....

#82914 - Darkflame - Thu May 11, 2006 3:45 am

poslundc wrote:
Darkflame wrote:
Euler is sooo outdated :-/

S=ut is easy and more accurate.


If it's so easy then why can't you seem to get it working? ;-)

In all seriousness, the Euler method is better. It is very, very difficult to design a stateless system of composite forces that is at all scalable or generic. State-tracking is far more sensible, if your intent is to make a game, anyway.

Accuracy is not a problem in games, either. At least, if you use the value 9.81 m/s/s in 16.16 fixed-point format, then at 60 ticks per second your error will be 0.000122 m (or about 0.1 millimetres) per second.

If you're satisfied with the accuracy of 9.81 m/s/s as your gravitational constant but not satisfied with an error of 0.1 mm/s, then you probably shouldn't be designing games for computer systems.

Dan.


I dont get you here :-/
Sorry...by "Eular" I was thinking liner. (as in the guys jumps wouldnt curve..and bounces would be impossible to do without a lot of extra code)

I was using that equation because it was...well..the real equation for accelerateing objects. It wasnt an accuracy issue at all.
Its just my instict says you shouldnt have a "if ball is at top then start going down" bit...it should be a smoother,curved motion.



I still dont get whats wrong with that equation.
It worked fine in all the other code I used.

I did a game with a bounceing ball using that code and it worked perfectly.

(you simply u=-u when it hits the ground)

My only problem with helping dan is I have no knowledge of gbadev.
I dont even know what varibles of his are interger.

#82916 - Dan2552 - Thu May 11, 2006 3:51 am

Quote:

I dont even know what varibles of his are interger.


Don't worry, neither do I :D

#82932 - poslundc - Thu May 11, 2006 5:55 am

OK, I've taken another look at your code, and it's problematic to say the least. I strongly recommend you work through some basic C programming tutorials before diving headfirst into GBA programming. Some fundamental things to be aware of:

- Your use of "goto" breaks the structure of your program. Goto is almost obsolete with the advent of structured programming languages, and should only be used in exceptional circumstances.

- ((1/2)*a*t*t) will always evaluate to 0. (Excerise: figure out why.)

- You are #including non-header C files in your C program. That's bad form and error prone.

Those are just a few of the top-level issues you need to deal with. At the very least your program flow is difficult to follow and needs to be cleaned up. There are plenty of good tutorials and demos to help you with this.

EDIT:

Darkflame wrote:
I dont get you here :-/
Sorry...by "Eular" I was thinking liner. (as in the guys jumps wouldnt curve..and bounces would be impossible to do without a lot of extra code)

I was using that equation because it was...well..the real equation for accelerateing objects. It wasnt an accuracy issue at all.
Its just my instict says you shouldnt have a "if ball is at top then start going down" bit...it should be a smoother,curved motion.


Instead of trying to determine where the object should be at any given time using a general equation to describe its behaviour (sometimes known as "tweening"), try storing the following three vectors:

- position
- velocity
- acceleration

Then every frame, update all three of them based on their relationship to each other.

That's Euler integration.

Dan.

#82941 - Ultima2876 - Thu May 11, 2006 9:41 am

Why do you want s = ut anyway? Acceleration due to gravity isn't 9.81, it's 9.81 m/s/s. That means that it's NOT 9.81 pixels per frame, unless a frame indicates a second of time (which it doesn't, a frame is 1/59.97 of second) and a pixel represents a metre, which it probably doesn't.

Anyway, nitpicking aside, s = ut won't be very good on a pixel level anyway, because you don't need that level of accuracy.

Also, you're doing 1/2. That evaluates to 0 because it's not fixed point, so the result of your ((1/2) * a * t * t) will always be 0. Use something like ((128 * a * t * t) >> 8) instead - which cancels to ((a * t * t) >> 1).

#82952 - tepples - Thu May 11, 2006 1:33 pm

But some games have metres as a fundamental game unit. For instance, Super Mario Bros. and several other platformers seem to count one metre for each 16-pixel metatile.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#82973 - Dan2552 - Thu May 11, 2006 4:58 pm

Quote:
- You are #including non-header C files in your C program. That's bad form and error prone.


it's exactly what the HAM tutorial told me to do. (except loads and loads of them :p)

_

everyone always says try tutorials first, people told me to do that on VB, and Delphi. I ignored - I taught myself pretty well, with the exception that my code will look messy to others (believe me, others looks messy to me). For VB my 1st main project was an IRC client - that worked out pretty well, and for delphi my main project is an IM client which takes advantage of IRC server but not in the same way (anyway that doesnt matter..)

well what i'm trying to say, I seem to learn better by diving in then reading tutorials. I'm an odd one :)
_



and on a side note: I don't care how BAD 'goto' is, it happens to be my favourate workaround for every single language I've done (VB, Delphi, C). I LIKE IT :D although I havn't used it much in delphi recently


_

Can someone just suggest to me a very simple BUT WELL WORKING jump method, i like the smoothness from Kirby :) I want the jump working well with moving sideways

#82979 - Ultima2876 - Thu May 11, 2006 5:29 pm

Ultima2876 wrote:
probably


tepples wrote:
But some games have metres as a fundamental game unit. For instance, Super Mario Bros. and several other platformers seem to count one metre for each 16-pixel metatile.


;P

#82980 - poslundc - Thu May 11, 2006 5:30 pm

Dan2552 wrote:
Quote:
- You are #including non-header C files in your C program. That's bad form and error prone.


it's exactly what the HAM tutorial told me to do. (except loads and loads of them :p)


Unfortunately, the majority of GBA dev tutorials are erroneous to some degree.

TONC is the most reliable of any of them.

Quote:
well what i'm trying to say, I seem to learn better by diving in then reading tutorials. I'm an odd one :)


Lot's of people do. It's a good way to learn. Unfortunately, you are trying to learn how to run before learning how to crawl. Take a step back and review the basics.

You are unlikely to find anyone in this forum with the time or energy to hold your hand through getting a physics model working when you aren't up to the basics of, say, knowing which of your variables are integers.

Quote:
and on a side note: I don't care how BAD 'goto' is, it happens to be my favourate workaround for every single language I've done (VB, Delphi, C). I LIKE IT :D although I havn't used it much in delphi recently


Do you like not being able to debug your code? Do you like having slow code that the compiler is unable to optimize? Do you like making your code infinitely harder to read for the people you want help from?

If you refuse to learn how to use the basic tools the language is providing you with, you're only screwing yourself.

Quote:
Can someone just suggest to me a very simple BUT WELL WORKING jump method, i like the smoothness from Kirby :) I want the jump working well with moving sideways


I wrote:
Instead of trying to determine where the object should be at any given time using a general equation to describe its behaviour (sometimes known as "tweening"), try storing the following three vectors:

- position
- velocity
- acceleration

Then every frame, update all three of them based on their relationship to each other.


And if that's not enough for you to go on, please search the forums as this topic has already been discussed inside and out.

Dan.

#82981 - Dan2552 - Thu May 11, 2006 5:40 pm

poslundc wrote:

Do you like not being able to debug your code?

hmm? If a problem comes up when compiling, i check the debug messages and it tells me the problem, goto doesn't prevent this

poslundc wrote:

Do you like having slow code that the compiler is unable to optimize?

the game runs fine on emulator/real hardware, why does it need to be optimized?

Quote:
Do you like making your code infinitely harder to read for the people you want help from?

OK. I never planned to release the source, all i asked from Darkflame was a jumping method which actually worked with being able to jump sideways aswell.

I make the code readable for myself, not for others, I don't post my code very often.

Quote:

If you refuse to learn how to use the basic tools the language is providing you with,

I'm not refusing, this is my first C project, surely I can take some shortcuts on my 1st game? (except when i tested moving sprite around, pressing button and stuff happening when sprite is over a button, etc.)

Quote:

you're only screwing yourself.

how is that possible :p ?

#82988 - keldon - Thu May 11, 2006 6:36 pm

I wouldn't hold your hand but maybe I can give you some easy direction into getting your physics engine working.

Check out sodaconstructor. Their physics engine is under 30 lines of code. I had to implement a clone (minus the ability to walk and grip stuff because they semi-reflective collisions were complicated).

Honestly physics engines are one of them things that are simply simple once you've got it working.

----

I have to go now but there are simple steps in implementing a physics engine. Just create a simple joypad handler that will apply the force to an object; and eventually your physics engine will enable it to work.

When I get back I can give a shot at helping unless someone beats me to it.

#82999 - Dan2552 - Thu May 11, 2006 8:56 pm

heres what I had before I asked Darkflame:
(before I look TOO much like an idiot)

Code:


void gravity() {
if (jumping == 1) {
if (frodo1_y > 100 - (jumpheight)) {
  frodo1_y -= speedfall;
 
  }
 if (frodo1_y <= 100 - (jumpheight)) {
jumping = 0;
  }
}
if (jumping == 0) {
  if (frodo1_y < 100) {
  frodo1_y += speedfall;
  }
}

}



Code:


if(F_CTRLINPUT_UP_PRESSED)   {

if (frodo1_y == 100) { //ground level
jumping = 1;
}
return;
}




This KIND OF works well, but it doesn't seem very smooth for me.. but then maybe it will look smoother when animation is added?

#83006 - poslundc - Thu May 11, 2006 11:07 pm

The system you've designed there works for constant velocity, where the acceleration is zero.

Gravity is a force of constant acceleration, though, and changing velocity.

The next step is to change your falling speed every tick by the rate of acceleration due to gravity.

But for the love of pete...

- Don't use magic numbers in your code. Use macros or constants with meaningful names instead. The number 100 could mean anything, and when you have it in four places, that's just a bunch of opportunities for you to miss something and cause an error if you need to change the value.

- Learn to use fixed point math. Search the forum or google it.

Dan.

#83028 - tepples - Fri May 12, 2006 4:49 am

Dan2552 wrote:
poslundc wrote:
Do you like having slow code that the compiler is unable to optimize?

the game runs fine on emulator/real hardware, why does it need to be optimized?

Optimized code uses fewer CPU cycles, which means more time spent in VBlankIntrWait(), which means less current drawn from the battery. And what happens when you want to add features later on?

Dan2552 wrote:
poslundc wrote:
you're only screwing yourself.

how is that possible :p ?

Wikipedia explains (not safe for work)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#83047 - Dan2552 - Fri May 12, 2006 10:29 am

poslundc wrote:
The system you've designed there works for constant velocity, where the acceleration is zero.

Gravity is a force of constant acceleration, though, and changing velocity.

The next step is to change your falling speed every tick by the rate of acceleration due to gravity.

ok.
so everytime I press up, reset the speed
when player jumps to its highest, trigger a speed increasing thingy?

Do I want to slow the jump aswell or wont that matter much?

Quote:

- Don't use magic numbers in your code. Use macros or constants with meaningful names instead. The number 100 could mean anything, and when you have it in four places, that's just a bunch of opportunities for you to miss something and cause an error if you need to change the value.

- Learn to use fixed point math. Search the forum or google it.

Dan.


-So I should declare a floorlevel and set it to 100, or am i missing something :(

-

#83048 - keldon - Fri May 12, 2006 10:51 am

Quote:
-So I should declare a floorlevel and set it to 100, or am i missing something :(

Yes.

#83055 - sgeos - Fri May 12, 2006 12:56 pm

You should be doing something like this:
Code:
#define FLOOR_LEVEL 100
#define TRUE 1
#define FALSE 0

int hitFloor(int pY)
{
  if (pY < FLOOR_LEVEL)
    return TRUE;
  return FALSE;
}

void snapToFloor(dude_t *pDude)
{
    pDude->y = FLOOR_LEVEL;
    pDude->jumping = FALSE;
}

void floorCheck(dude_t *pDude)
{
  if ( hitFloor(pDude->y) )
    snapToFloor(pDude);
}

void caller(void)
{
  input_t input;
  dude_t dude;

  initDude(&dude);
  initInput(&input);

  while ( TRUE )
  {
    getInput(&input);
    updatePosition(&dude, &input);
    floorCheck(&dude);
  }
}

Why is that nifty? Well... let's say that for technical reasons you have to change the floor level to 123. I dare you find all the places where you need to change the above code to get a floor level of 123. Assume that all of the code I failed to show you is written in a similar fashion. =)

-Brendan