Ray Casting Class Day(year) Report on 12/31

The whole class was rewritten. and this time I will do step by step until each function I wrote is tested.
This version still can't do anything about ray casting, but it created a map system that is great for debugging. what I have learned today about coding is read the manual and know what each function does. Like tan() take in radian instead of degree. so deg2rad() will be useful.
After the completion of this, debugging will be a lot easier because I can get a 2D view of the point of view.. I just need to include the field of view in(should be quite soon)
Here is what the map view looks like right now.
raycasting map
And here is the code generates everything.

set_time_limit(5);
class raycasting{
	var $map;
	var $pov;
	var $ceiling = 64;
	var $direction;
	var $scale = 32;
	var $map_image;
	var $color;
//function that import a map
function create_map($string){
	$string = explode("\n",$string);
	$i = 0;
	$count = count($string);
	while($i<$count){
		$string[$i] = explode(' ', $string[$i]);
		++$i;
	}
	//now, it should created an array hold the map, it is acessed like $array[$x][$y]
	$this->map = $string;
}
 
function iswall($x, $y){
	if($this->map[floor($x/$this->scale)][floor($y/$this->scale)]){
		return true;
	}else{
		return false;
	}
}
function create_pov($x, $y, $z, $a){
	//$x, $y will be where the character at on the 2D map. $a is the viewing angle
	//don't care about $z, for now $z is half of the scale
	//each value have to be interger except $a. it also have to exsit inside the map
	$border = count($this->map)*$this->scale;
	if($x>$border){
		die('outside border');
	}else{
		$x = abs(round($x));
	}
	$border = count($this->map[0])*$this->scale;
	if($y>$border){
		die('outside border');
	}else{
		$y = abs(round($y));
	}
	//$x, $y can't be in a wall
	if($this->iswall($x, $y)){
		die('this is wall');
	}
	if($z>$this->ceiling){
		$z = $this->ceiling;
	}else{
		$z = abs(round($z));
	}
	//angle treated differently
	while($a < 0){
		$a += 360;
	}
	while($a >= 360){
		$a -= 360;
	}
	//test for y increase or decrease
	if (180>$a){
		$this->direction['y'] = -1;
	}elseif(180<$a){
		$this->direction['y'] = 1;
	}
	//test for x increase or decrease
	if (270>$a && 90<$a){
		$this->direction['x'] = -1;
	}elseif(($a>270 or $a<90) or $a == 0){
		$this->direction['x'] = 1;
	}
	//so if it's 90, 270 ,180 or 0 degree, x or y can be 0;
 
	//create the tri_a, the angle for the triangle it creates
	$tri_a = $a;
	while($tri_a >= 90){
		$tri_a -= 90;
	}
	//post everything as an array for pov
	$this->pov = array('x' => $x,
						'y' => $y,
						'z' => $z,
						'a' => $a,
						'tri_a' => $tri_a);
}
 
function create_map_image(){
 
	$size = imagesx($this->map_image);
	//find the angle to draw
	if($this->direction['x'] == 1 && $this->direction['y'] == 1){
		$x = $size/2 + tan(deg2rad($this->pov['tri_a']))*$size/2;
		$y = $size;
	}elseif($this->direction['x'] == -1 && $this->direction['y'] == -1 ){
		$x = $size/2 - tan(deg2rad($this->pov['tri_a']))*$size/2;
		$y = 0;
	}elseif($this->direction['x'] == -1 && $this->direction['y'] == 1){
		$x = 0;
		$y = $size/2 + tan(deg2rad($this->pov['tri_a']))*$size/2;
	}elseif($this->direction['x'] == 1 && $this->direction['y'] == -1 ){
		$x = $size;
		$y = $size/2 - tan(deg2rad($this->pov['tri_a']))*$size/2;
	}elseif($this->direction['x'] == 0 && $this->direction['y'] == -1){
		$x = $size/2;
		$y = 0;
	}elseif($this->direction['x'] == 0 && $this->direction['y'] == 1){
		$x = $size/2;
		$y = $size;
	}elseif($this->direction['x'] == 1 && $this->direction['y'] == 0){
		$x = $size;
		$y = $size/2;
	}elseif($this->direction['x'] == -1 && $this->direction['y'] == 0){
		$x = 0;
		$y = $size/2;
	}
	//draw angle
	imageline($this->map_image, $size/2, $size/2, $x, $y, $this->color[1]);
	//draw map
	//draw grid
	$x= $this->pov['x'];
	$y= $this->pov['y'];
	//find where $x is in one scale grid;
	while($x > $this->scale){
		$x -= $this->scale;
	}
	while($y > $this->scale){
		$y -= $this->scale;
	}
	//plot user
	//color
	$user_color = imagecolorallocate($this->map_image, 0, 255, 255);
	//plot
	imagerectangle($this->map_image, $size/2-3,$size/2-3,$size/2+3,$size/2+3, $user_color);
	//plot grid;
	$grid_y = $size/2 + $this->scale - $y;
	while($grid_y > 0){
		$grid_y -= $this->scale;
		imageline($this->map_image, 0, $grid_y, $size, $grid_y, $this->color[1]);
	}
	$grid_y = $size/2 - $y;
	while($grid_y < $size){
		$grid_y += $this->scale;
		imageline($this->map_image, 0, $grid_y, $size, $grid_y, $this->color[1]);
	}
	$grid_x = $size/2 - $x;
	while($grid_x < $size){
		$grid_x += $this->scale;
		imageline($this->map_image, $grid_x, 0, $grid_x, $size, $this->color[1]);
	}
	$grid_x = $size/2 + $this->scale - $x;
	while($grid_x > 0){
		$grid_x -= $this->scale;
		imageline($this->map_image, $grid_x, 0, $grid_x, $size, $this->color[1]);
	}
	//find which piece the item is on
		$scale_x= floor($this->pov['x']/$this->scale);
		$scale_y= floor($this->pov['y']/$this->scale);
	//color the wall present
	$i = 0;
	$count = count($this->map);
	$count2 = count($this->map[0]);
 
 
	$i = 0;
	while($i < $count){
		$n = 0;
		while($n < $count2){
	if($this->map[$i][$n]){
					$wall_x = $size/2 + ($i-$scale_x)* $this->scale - $y;
					$wall_y = $size/2 + ($n-$scale_y)* $this->scale - $x;
					imagefilledrectangle($this->map_image, $wall_y, $wall_x, $wall_y + $this->scale, $wall_x + $this->scale, $this->color[1]);
	}
			++$n;
		}
		++$i;
	}
}
}
 
$world = new raycasting;
$map = 
"1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 1 0 1 1 0 0 1
1 1 1 1 1 1 1 1";
$world->create_map($map);
 
$world->map_image = imagecreate(400, 400);
 
 
	$world->color[] = imagecolorallocate($world->map_image, 0, 0, 0);
	$world->color[] = imagecolorallocate($world->map_image, 255, 255, 255);
 
	$world->create_pov(33,33,65,60);
	$world->create_map_image();
 
//include_once('dbug.php');
//new dbug($world->map);
//new dbug($world->pov);
//new dbug($world->direction);
 
header("Content-type: image/gif");
imagegif($world->map_image);
imagedestroy($world->map_image);

Last post of the year.. Happy new year!

:hitfly1:

Joe's picture


Interesting

What does this actually

Anonymous's picture

What does this actually do??

It's like a debug system for

Mgccl's picture

It's like a debug system for the ray casting system...
it present the 2D map...

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar. If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.

More information about formatting options

What is 20 + 58?
To combat spam, please solve the math question above.
Honey Pot that kill bots