Jump to content
  • Advertisement
Sign in to follow this  
ToastFlambe

Wierd variable problem. How is this possible?

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First, here's the code.
int main (int argc, char *argv[])
{
     //start setup!

     int ScreenWidth = 800;
     int ScreenHeight = 600;


     if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) <0 )
     {
       printf("Unable to init SDL: %s\n", SDL_GetError());
       return 1;
     }


     SDL_Surface *screen = NULL;

     screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight, 32,SDL_HWSURFACE | SDL_DOUBLEBUF);

     if ( screen == NULL )
     {
       printf("Unable to set 640x480 video: %s\n", SDL_GetError());
       return 1;
     }

     //now for the variables

     //player variables
     float posX = 5, posY = 10;                  //player's x and y
     float dirX = 1, dirY = 0;                   //player's direction vector, always ones
     float planeX = 0, planeY = -0.50;             //plane vector x and y, currently set for 60 degrees
     float x, y;                                   //ray's x and y intersection values

     //FPS counter variables
     float time = 0;
     float oldtime = 0;

     //some temporary wall start and end point variables. Later these values will be gotten
     //from an array. Also a color

     int WallStartX = 0, WallStartY = 20, WallEndX = 20, WallEndY = 0, WallHeight = 32;
     int yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);
     int black = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);


     int done = 0;

          while(!done)
          {
               SDL_Delay(5);                          //delay 5 millis

               SDL_FillRect(screen, NULL, black);

               for(int w = 0; w < ScreenWidth; w++)
               {
                    float cameraX = 2 * w / ScreenWidth - 1;               //screen coord represented by a vector
                    float RayDirX = (dirX + planeX * cameraX) + posX;   //that vector is used to find the X
                    float RayDirY = (dirY + planeY * cameraX) + posY;   //and Y values of the Ray's direction

                    if(w == 0)
                    {
                         printf("CameraX is %d\n RayDirX is %d\n  RayDirY is %d\n", cameraX, RayDirX, RayDirY);
                    }


Now the answer should be this: CameraX is -1 RayDirX is 6 RayDirY is 10.5 But I get this CameraX is 0 RayDirX is -1074790400 RayDirY is 0 I've checked the equations and they're fine, so I have no clue what is causing this. I haven't even messed around with these variables anywhere else in the program yet! I omitted the includes and stuff when I posted this so that's not the problem.

Share this post


Link to post
Share on other sites
Advertisement
I believe the variables are just displayed wrong.

Instead of %d:
printf("CameraX is %d\n RayDirX is %d\n RayDirY is %d\n", cameraX, RayDirX, RayDirY);

Use %f:
printf("CameraX is %f\n RayDirX is %f\n RayDirY is %f\n", cameraX, RayDirX, RayDirY);

Share this post


Link to post
Share on other sites
What compiler are you using? For GCC and friends add -Wall to your compile options so it'll yell at you:


foo.cpp:15: warning: int format, double arg (arg 2)
foo.cpp:15: warning: int format, double arg (arg 3)
foo.cpp:15: warning: int format, double arg (arg 4)

Share this post


Link to post
Share on other sites
Quote:
Original post by UltimaX
I believe the variables are just displayed wrong.

Instead of %d:
printf("CameraX is %d\n RayDirX is %d\n RayDirY is %d\n", cameraX, RayDirX, RayDirY);

Use %f:
printf("CameraX is %f\n RayDirX is %f\n RayDirY is %f\n", cameraX, RayDirX, RayDirY);


That is indeed the issue, and one I've encountered myself, and fixed.

Share this post


Link to post
Share on other sites
You have fallen victim to one fo printf & co.'s main flaws: utter lack of type safety (unless you depend on compiler-specific warnings). This wouldn't have happened if you were using C++ and it's classes... e.g.:

"original" code:

int x,y,z;
printf( "%d,%d,%d" , x , y , z ); //OK, used %d for int
cout << x << "," << y << "," << z; //OK, passed ints


"modified" code:

double x,y,z; //we decided to change the type of x,y & z
printf( "%d,%d,%d" , x , y , z ); //Whoops!! We frogot to change this line, misbehaves now!!
cout << x << "," << y << "," << z; //OK, dosn't need to be changed.


For those who just are addicted to printf style formatting, there's an alternative:

#include <boost/format.hpp>
double x,y,z;
using namespace boost;
using namespace std;

cout << format( "%d,%d,%d" ) % x % y % z << endl; //should work even though we used %d


See http://boost.org/index.htm for information on the boost library.

Also, you've mis-spelled weird, which is one of my personal pet peeves. Here's the diddy I memorized back in grade school: "I before E, except after C, or when sounded like Ay as in Neighbor and Weigh. Science and seize, do as they please, and weird is just weird."

(I had frogotten the words used in the "___ and ___, do as they please" bit, Conner McCloud was kind enough to lend me the words he learned :-) ).

[Edited by - MaulingMonkey on April 25, 2005 4:34:56 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Also, you've mis-spelled weird, which is one of my personal pet peeves. Here's the diddy I memorized back in grade school: "I before E, except after C, or when sounded like Ay as in Neighbor and Weigh. (something) and (something), do as they please, and weird is just weird."

(unfortunately I froget which two words were listed in the something and something bit).


froget? :P

Share this post


Link to post
Share on other sites
Quote:
Original post by kappa
Quote:
Original post by MaulingMonkey
Also, you've mis-spelled weird, which is one of my personal pet peeves. Here's the diddy I memorized back in grade school: "I before E, except after C, or when sounded like Ay as in Neighbor and Weigh. (something) and (something), do as they please, and weird is just weird."

(unfortunately I froget which two words were listed in the something and something bit).


froget? :P


Fixed. Weird is my pet peeve, not forget :P.

Share this post


Link to post
Share on other sites
BTW, there's yet another problem:
Quote:
float cameraX = 2 * w / ScreenWidth - 1;

CameraX will be -1 for any w in [0, ScreenWidth), because both operands of the division are integers. One or both of them must be floats, the easiest way in this case is probably changing the number 2 into a floating-point literal:
float cameraX = 2.f * w / ScreenWidth - 1;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!