var game_map = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1], [1,0,0,1,1,1,1,1,1,0,0,0,0,1,0,1], [1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1], [1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1], [1,0,0,1,1,1,1,1,1,0,1,1,1,1,0,1], [1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1], [1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ] var mapWidth = 200 var orignal_map_width = 400 var playerx = 50 var playery = 50 var player_dir = Math.PI/ 2 var square_edge_len = mapWidth/game_map.length var player_move_mult = mapWidth/orignal_map_width * 10 var ray_trace_speed = mapWidth/orignal_map_width function setup() { createCanvas(window.innerWidth, window.innerHeight); frameRate(24) //determines wether if the screen size is larger than 1000 px, this will allow if the program is within a small canvas if(window.innerWidth > 1000){ document.getElementById("defaultCanvas0").style.marginLeft = "25%"; } } wallGrid = new Uint8Array(8); document.onkeydown = function(event){ if(event.keyCode == 87){ future_playerx = Math.sin(player_dir)*player_move_mult future_playery = Math.cos(player_dir)*player_move_mult if(0 == game_map[Math.floor(game_map.length*(future_playery+playery)/mapWidth)][Math.floor(game_map.length*(future_playerx+playerx)/mapWidth)]){ playerx += future_playerx playery += future_playery } } if(event.keyCode == 83){ future_playerx = Math.sin(player_dir)*player_move_mult future_playery = Math.cos(player_dir)*player_move_mult if(0 == game_map[Math.floor(game_map.length*(-future_playery+playery)/mapWidth)][Math.floor(game_map.length*(-future_playerx+playerx)/mapWidth)]){ playerx -= future_playerx playery -= future_playery } } if(event.keyCode == 65){ player_dir += 1*Math.PI/100 } if(event.keyCode == 68){ player_dir -= 2*Math.PI/100 } } function draw() { background(255) var height_mult = 2 function make_polygon(left_length,right_length,poly_width,x_position,color){ fill(color) noStroke() quad(x_position,left_length*height_mult,x_position,(400-left_length)*height_mult,poly_width+x_position+1,(400-right_length)*height_mult,poly_width+x_position+1,right_length*height_mult) return(poly_width) } //make_polygon(0,120,10,0) //draw ray resetMatrix() var future_intersect_x = Math.cos(-player_dir+Math.PI/2)*200 +playerx var future_intersect_y = Math.sin(-player_dir+Math.PI/2)*200 +playery var ending_x = Math.floor(game_map.length*(future_intersect_x)/mapWidth) var ending_y = Math.floor(game_map.length*(future_intersect_y)/mapWidth) var FOV = 60 var raysTocount = 500 var degperray= FOV/raysTocount var first_side_ray = [] var second_side_ray = [] var postion = [] var x_pos_on_canvas = 0 var width_per_cast = window.innerHeight/raysTocount noStroke() fill([179,179,179]) rect(0,200*height_mult,window.innerHeight,400*height_mult) fill([100,100,100]) rect(0,0,window.innerHeight,200*height_mult) for(var RayNum = 0;RayNum < raysTocount;RayNum++){ var RayAng = ((-player_dir+Math.PI/2)*(180/Math.PI) - (FOV/2))+ RayNum*(degperray) var TrueRayAng = (RayAng % 360 < 0) ? 360+(RayAng % 360) : RayAng % 360 var isLeft = TrueRayAng > 90 & TrueRayAng < 270 var raymod =(isLeft) ? - playerx % square_edge_len : square_edge_len - playerx % square_edge_len var RayCoef = Math.tan(TrueRayAng*Math.PI/180) var Y = playery + raymod* RayCoef dX = (isLeft) ? -square_edge_len : square_edge_len var X = playerx + raymod dY = dX * RayCoef while(true){ LineYGrid = Math.floor((Y) / square_edge_len) LineXGrid =(isLeft) ? Math.floor((X) / square_edge_len) - 1 : Math.floor((X) / square_edge_len) if(LineXGrid < 16 && LineXGrid >= 0 && LineYGrid < 16 && LineYGrid >= 0) { if(game_map[LineYGrid][LineXGrid] != 0){ //rect(square_edge_len*LineXGrid,square_edge_len*LineYGrid,square_edge_len,square_edge_len) break } }else{ break } X += dX Y += dY } var distance_found = Math.sqrt((playerx-X)**2+(playery-Y)**2) first_side_ray = [X,Y,distance_found] //line(playerx,playery,X,Y) RayAng += 90 //+ ((-player_dir+Math.PI/2)*(180/Math.PI) - (FOV/2))+ RayNum*(degperray) var TrueRayAng = (RayAng % 360 < 0) ? 360+(RayAng % 360) : RayAng % 360 var isTop = (TrueRayAng > 90 & TrueRayAng < 270) var raymod =(isTop) ? square_edge_len - playery % square_edge_len : - playery % square_edge_len var RayCoef = -Math.tan(TrueRayAng*Math.PI/180) dY = (isTop) ? square_edge_len : -square_edge_len dX = dY * RayCoef var Y = playery + raymod var X = playerx + raymod* RayCoef while(true){ LineYGrid =(isTop) ? Math.floor((Y) / square_edge_len) : Math.floor((Y) / square_edge_len)- 1 LineXGrid = Math.floor((X) / square_edge_len) if(LineXGrid < 16 && LineXGrid >= 0 && LineYGrid < 16 && LineYGrid >= 0) { if(game_map[LineYGrid][LineXGrid] != 0){ //rect(square_edge_len*LineXGrid,square_edge_len*LineYGrid,square_edge_len,square_edge_len) break } }else{ break } X += dX Y += dY } var distance_found = Math.sqrt((playerx-X)**2+(playery-Y)**2) var cos_change = Math.cos((degperray/RayNum - FOV/2)* Math.PI/180) if (distance_found < first_side_ray[2] ){ line(playerx,playery,X,Y) postion.push([playerx,playery,X,Y]) second_line = distance_found if(RayNum == 0){ first_line = second_line } x_pos_on_canvas += make_polygon(first_line*cos_change,second_line*cos_change,width_per_cast,x_pos_on_canvas,[77,166,255]) first_line = second_line } else{ line(playerx,playery,first_side_ray[0],first_side_ray[1]) distance_found = first_side_ray[2] postion.push([playerx,playery,first_side_ray[0],first_side_ray[1]]) second_line = first_side_ray[2] if(RayNum == 0){ first_line = second_line } x_pos_on_canvas += make_polygon(first_line*cos_change,second_line*cos_change,width_per_cast,x_pos_on_canvas,[25,217,255]) first_line = second_line } // old line renderer // strokeWeight(width/FOV); // var lens_affect_distance = distance_found * Math.cos((RayNum - FOV/2)* Math.PI/180) // stroke(200-lens_affect_distance,200-lens_affect_distance,200-lens_affect_distance); // wall_height = 200 // lens_affect_distance = (lens_affect_distance > 200) ? 200 :lens_affect_distance // lens_affect_distance = (lens_affect_distance < 0) ? 0 :lens_affect_distance // line((width/FOV)*RayNum,lens_affect_distance,(width/FOV)*RayNum,height - (lens_affect_distance)) // strokeWeight(1) } var gamemap_len = game_map.length noStroke() for(y = 0; y < gamemap_len;y++){ for(x = 0; x < gamemap_len;x++){ if(game_map[y][x] == 1){ fill([100]); } else{ fill([255]); } rect(square_edge_len*x,square_edge_len*y,square_edge_len,square_edge_len) } } //minimap renderer var poslen = postion.length; stroke(0) for(var raycastindex = 0 ;raycastindex < poslen; raycastindex++){ rayinfo = postion[raycastindex] line(rayinfo[0],rayinfo[1],rayinfo[2],rayinfo[3]) } fill([255,0,0]); translate(playerx,playery) rotate(-player_dir) triangle(-2.5, -5, 2.5,-5, 0, 5); resetMatrix() //creates background }