View Single Post
  #1  
Old 08-17-2016, 04:36 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Raycasting V2 - Progress!!!

Okay! So I have sort of made some progress with my efforts in raycasting using GS2, it's still very buggy and still doesn't look quite right...



PHP Code:
//#CLIENTSIDE
function onCreated(){
  
this.maxlooplimit = 100000;
  
sleep(0.5);
  
this.map = {
    {
1,1,1,1,1,1,1,1,1,1,1,1,1},
    {
1,0,0,0,0,0,0,0,0,0,2,0,1},
    {
1,0,0,0,0,0,0,0,0,0,2,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,1,1,0,0,0,0,0,2},
    {
1,0,0,0,0,1,1,0,0,0,0,0,2},
    {
2,0,0,0,0,0,0,0,0,0,0,0,1},
    {
2,0,0,0,0,0,0,0,0,0,0,0,1},
    {
2,0,0,0,0,0,0,0,0,0,0,0,1},
    {
2,0,0,0,0,2,2,0,0,0,0,0,1},
    {
2,0,0,0,0,2,2,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,1,1,1,1,1,2,2,2,1,1,1,1},
  };
  
this.w = 600;
  
this.h = 400;
  
this.off = {screenwidth/2 - this.w/2, screenheight/2 - this.h/2};
  
this.off = {50, 50};
  
this.mapwidth = this.map[0].size();
  
this.mapheight = this.map.size();
  
this.posx = 2;
  
this.posy = 2;
  
this.dirx = -1;
  
this.diry = 0;
  
this.planex = 0;
  
this.planey = 0.66;
  
this.time = 0;
  
this.oldtime = 0;
  for(
temp.i = 0; temp.i < 2000; temp.i ++){
    if(
isobject((@"3D_R_"@temp.i))){
      (@
"3D_R_"@temp.i).destroy();
    } 
  }
  
onTimeout();
}
function 
sqrt(n) { 
  return 
n^0.5; 
}
function 
onTimeout(){
  for(
temp.x = 0; temp.x < this.w; temp.x ++){
    
temp.camerax = 2 * temp.x/this.w -1;
    
temp.rayposx = this.posx;
    
temp.rayposy = this.posy;
    
temp.raydirx = this.dirx + this.planex * temp.camerax;
    
temp.raydiry = this.diry + this.planey * temp.camerax;
    
temp.mapx = int(temp.rayposx);
    
temp.mapy = int(temp.rayposy);
    
temp.sidedistx = 0;
    
temp.sidedisty = 0;
    
temp.deltadistx = sqrt(1 + (temp.raydiry * temp.raydiry)/(temp.raydirx * temp.raydirx));
    
temp.deltadisty = sqrt(1 + (temp.raydirx * temp.raydirx)/(temp.raydiry * temp.raydiry));
    
temp.perpwalldist = 0;
    
temp.stepx = 0;
    
temp.stepy = 0;
    
temp.hit = 0;
    
temp.side = 0;
    if(
temp.raydirx < 0){
      
temp.stepx = -1;
      
temp.sidedistx = (temp.rayposx - temp.mapx)*temp.deltadistx;
    }else{
      
temp.stepx = 1;
      
temp.sidedistx = (temp.mapx + 1 - temp.rayposx) * temp.deltadistx;
    }
    if(
temp.raydiry < 0){
      
temp.stepy = -1;
      
temp.sidedisty = (temp.rayposy - temp.mapy) * temp.deltadisty;
    }else{
      
tmep.stepy = 1;
      
temp.sidedisty = (temp.mapy + 1 - temp.rayposy) * temp.deltadisty;
    }
    
temp.count = 0;
    while(
temp.hit == 0){
      
temp.count ++;
      if(
temp.sidedistx < temp.sidedisty){
        
temp.sidedistx += temp.deltadistx;
        
temp.mapx += temp.stepx;
        
temp.side = 0;
      }else{
        
temp.sidedisty += temp.deltadisty;
        
temp.mapy += temp.stepy;
        
temp.side = 1;
      }
      if(
this.map[temp.mapx][temp.mapy] > 0){
        
temp.hit = 1;
      }
      if(
temp.count == 9999){
        
temp.hit = 1;
        
//maxlooplimit..?
      
}
    }
    if(
temp.side == 0){
      
temp.perpwalldist = (temp.mapx - temp.rayposx + (1 - temp.stepx)/2)/temp.raydirx;
    }else{
      
temp.perpwalldist = (temp.mapy - temp.rayposy + (1 - temp.stepy)/2)/temp.raydiry;
    }
    
temp.lineheight = this.h/temp.perpwalldist;
    
temp.drawstart = -temp.lineheight/2 + this.h /2;
    if(
temp.drawstart < 0){
      
temp.drawstart = 0;
    }
    
temp.drawend = temp.lineheight/2  + this.h/2;
    if(
temp.drawend >= this.h){
       
temp.drawend = this.h - 1;
    }
    
temp.t = this.map[temp.mapx][temp.mapy];
    
temp.c = {0, 0, 0};
    if(
temp.t == 1){
      
temp.c = {1, 0, 0}; //Red for outside walls
    
}
    if(
temp.t == 2){
      
temp.c = {0, 1, 0}; //Blue for 2
    
}
    
temp.alp = 1;
    if(
temp.side == 1){
      
temp.alp = 0.5;
    }
    
//DRAWWWW
    
new GuiBitmapCtrl("3D_R_"@temp.x) {
      
x = thiso.off[0] + temp.x;
      
y = thiso.off[1] + temp.drawstart;
      
width = 1;
      
height = temp.drawend;
      
bitmap = "block.png";
      
alpha = temp.alp;
      
red = temp.c[0];
      
blue = temp.c[1];
      
green = temp.c[2];
    }
  }
  
temp.rot = 0;
  
temp.ms = 0.1;
  if(
keydown(1)) temp.rot = -0.1;
  if(
keydown(3)) temp.rot = 0.1;
  if(
keydown(2)){
    if(
this.map[int(this.posx - this.dirx * temp.ms)][int(this.posy)] == 0){
       
this.posx -= this.dirx * temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy - this.diry * temp.ms)] == 0){
      
this.posy -= this.diry * temp.ms;
    }
  }
  if(
keydown(0)){
    if(
this.map[int(this.posx + this.dirx * temp.ms)][int(this.posy)] == 0){
       
this.posx += this.dirx * temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy + this.diry * temp.ms)] == 0){
      
this.posy += this.diry * temp.ms;
    }
  }
  
temp.olddirx = this.dirx;
  
this.dirx = this.dirx * cos(-temp.rot) - this.diry * sin(-temp.rot);
  
this.diry = temp.olddirx * sin(-temp.rot) + this.diry * cos(-temp.rot);
  
temp.oldplanex = this.planex;
  
this.planex = this.planex * cos(-temp.rot) - this.planey * sin(-temp.rot);
  
this.planey = temp.oldplanex * sin(-temp.rot) + this.planey * cos(-temp.rot);
  
settimer(0.05);
} 
Would anyone have any ideas on how to make it look more realistic, maybe even texture/shading etc?

If I remove
PHP Code:
      if(temp.count == 9999){
        
temp.hit = 1;
        
//maxlooplimit..?
      
} 
I get: Script: Loop limit exceeded in function onTimeout in script of Weapon -Gunderak/3D

So guessing something isn't 100%..

EDIT: I was using this as a base and trying to convert it to GS2
http://lodev.org/cgtutor/files/raycaster_flat.cpp
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion

Last edited by Gunderak; 08-17-2016 at 06:01 AM..
Reply With Quote