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.

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!
Recent comments
3 days 17 hours ago
4 days 11 hours ago
4 days 19 hours ago
4 days 19 hours ago
5 days 10 hours ago
5 days 10 hours ago
6 days 4 hours ago
6 days 12 hours ago
1 week 19 hours ago
1 week 19 hours ago