Public Group

# Help implementing the raycasting tutorial on this site

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

## Recommended Posts

Specifically, This One
I have been reading that tutorial and banging out some code (in c\sdl) for the last three hours. Unfortnatly, It appears as though I'm missing something fundemental in it. Here is the (ugly) code, I've added alot of comments so people get a glimpse of what my thought process is.
[source lang = "c"]

#include <stdio.h>
#include <stdlib.h>
#include <SDL\SDL.h>
#include <math.h>

#include "libs\graphxlib.h"

int CheckCollisionX();
int CheckCollisionY(int x);
void move_player(int length, int direction);

int wall_x1 = 10; //all rather self explanatory
int wall_y1 = 10;
int wall_x2 = 50;
int wall_y2 = 90;
int wall_slope = 2;

int player_x = 10;
int player_y = 190;

int angle;
float ray_slope; //slope of the "shot out" ray

int int_x;   //x and y of the intercepted point
int int_y;

int x_diff;
int y_diff;

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(320,200,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_Event event;
Uint8* Keys;
for (;;){
while ( SDL_PollEvent(&event) )
{
if (event.type == SDL_QUIT )
{
break;
}
}
Keys = SDL_GetKeyState(0);
if (Keys[SDLK_ESCAPE]) {break;}
//sdl stuff ends here
//here be monsters!
for(angle = (player_heading - 30); angle != (player_heading + 30); angle+=1){ //the players view angle is 60 degrees, so the left half will be 60-30
ray_slope = tan(angle);  //shoot a ray
int_x = CheckCollisionX(); //check if it collides with wall
int_y = CheckCollisionY(int_x);
DrawRect(int_x, int_y,1, 32);  //if so, draw a single pixel thick line there
}
SDL_Flip(screen); //flip the buffer
}

return 0;
}

int CheckCollisionX(){
int b1, x;
b1 = (player_x - wall_x1) * wall_slope + (player_y - wall_y1);
x  = b1 / (ray_slope - wall_slope);
return x;
}

int CheckCollisionY(int x){
int y;
y  = wall_y1 + (x - wall_x1) * wall_slope;
return y;
}

void move_player(int length, int direction)
{
int x_unit, y_unit;

x_unit = cos(direction) * length;
y_unit = sin(direction) * length;

player_x += x_unit;
player_y += y_unit;
}


Basically, this ends up displaying this: | .| ...| (without the periods) A seemingly random (though always increasing x/decreasing y) pattern of lines. It seems as though its half correct. I think my problem may lie in how I draw it, or in my use of floats and ints. I will be eternally greatful to anyone who helps me out!

##### Share on other sites
Looks like my question was far too general. Here's some more info:
I haven't implemented wall scaling yet, though I doibt thats part of the problem. I really think the problem may be in my for loop, where I increase the angle measure, because the rays the camera shoot should pretty much always intersect with the wall-line, yet only a few wall segments are drawn. Also, I may be missing something fundemental on the drawing side, like instead of drawing a strip for each collision, should I be doing something different?
Anyway you put it, this'll be my only bump, and I'd really appreciate it if someone would help me wade through this mess.

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 13
• 13
• 14
• 68
• 15
• ### Forum Statistics

• Total Topics
632137
• Total Posts
3004314

×