View Single Post
  #1  
Old 11-23-2016, 10:40 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
GS2 3D Raycaster

There are a few things that need to be fixed/improved, I never fully finished sprite rendering (it sort of works lol).

Feel free to build on this, or play around with it etc.



I had some floor rendering, but it was extremely laggy as I couldn't access the image buffer directly.


PHP Code:
function onCreated(){
  
findplayer("Gund").addweapon(this);  //Change to your name
}
//#CLIENTSIDE
function onCreated(){
  
this.maxlooplimit = 2000000;
  
sleep(1);
  
this.map = {
    {
4,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4},
    {
1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
3,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
2,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,2,3,2,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,3,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,3,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,4,0,0,0,0,0,0,4,1,0,0,0,0,0,0,0,4,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,5},
    {
1,0,0,0,0,0,2,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {
1,1,1,1,1,1,2,3,4,5,6,7,8,1,1,1,1,1,1,1,1,1,1,1,1},
  };
  
this.sprites = {
    {
"bomb.png", 1, 3},
    {
"door.png", 3, 1},
  };
  
this.texturewidth = 64;
  
this.w = 700;
  
this.zbuffer = new[this.w];
  
this.h = 500;
  
this.off = {screenwidth/2 - this.w/2, screenheight/2 - this.h/2};
  
this.off = {70, 70};
  
this.mapwidth = this.map[0].size();
  
this.mapheight = this.map.size();
  
this.posx = 4;
  
this.posy = 3;
  
this.dirx = -1;
  
this.diry = 0;
  
this.planex = 0.221;
  
this.planey = 0.5;
  
this.time = 0;
  
this.steps = 0;
  
this.smode = -4;
  
this.wallcheck = 8;
  
temp.rotspeed = 0.08;
  
this.ntrig = {sin(temp.rotspeed), cos(temp.rotspeed)};
  
this.trig = {sin(-temp.rotspeed), cos(-temp.rotspeed)};
  if(
isobject("3D_R_GUN")){
    (@
"3D_R_GUN").destroy();
  }
  if(
isobject("Gunderak_3D")){
    (@
"Gunderak_3D").destroy();
  }
  
//return;
  
new GuiDrawingPanel("Gunderak_3D"){
    
useownprofile = true;
    
profile.fontcolor = "#FFFFFF";
    
width = thiso.w;
    
height = thiso.h;
    
x = thiso.off[0];
    
y = thiso.off[1];
    
clearall();
  }
  
onTimeout();
}
function 
sqrt(n) { 
  return 
n^0.5; 
}
function 
onTimeout(){
  
Gunderak_3D.drawimagestretched(0, 0, this.w, this.h/2, "gunderak_sky.png", 0, 0, 1, 1);
  
Gunderak_3D.drawimagestretched(0, this.h/2, this.w, this.h/2, "gunderak_floor.png", 0, 0, 1, 1);
  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{
      
temp.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.side == 0){
      
temp.perpwalldist = abs((temp.mapx - temp.rayposx + (1 - temp.stepx)/2)/temp.raydirx);
    }else{
      
temp.perpwalldist = abs((temp.mapy - temp.rayposy + (1 - temp.stepy)/2)/temp.raydiry);
    }
    
temp.lineheight = int(this.h/temp.perpwalldist);
    
temp.drawstart = (-temp.lineheight/2) + (this.h/2);
    
temp.drawend = (temp.lineheight/2)  + (this.h/2);
    
temp.t = this.map[temp.mapx][temp.mapy]-1;
    
temp.wallx = 0;
    if(
temp.side == 0){
      
temp.wallx = temp.rayposy + temp.perpwalldist * temp.raydiry;
    }else{
      
temp.wallx = temp.rayposx + temp.perpwalldist * temp.raydirx;
    }
    
temp.wallx -= int(temp.wallx);
    
temp.texx = int(temp.wallx *  this.texturewidth);
    if(
temp.side == 0 && temp.raydirx > 0){
      
temp.texx = this.texturewidth - temp.texx - 1;
    }
    if(
temp.side == 1 && temp.raydiry < 0){
      
temp.texx = this.texturewidth - temp.texx - 1;
    }
    
temp.texx = this.texturewidth - temp.texx - 1;
    
temp.c = {1, 1, 1};
    if(
temp.side == 1){
      
temp.c = {0.65, 0.65, 0.65};
    }
    
temp.h = temp.drawend - temp.drawstart;
    
Gunderak_3D.drawimagestretched(temp.x, temp.drawstart, 1, temp.h, "wolftextures.png", temp.texx+(thiso.texturewidth*temp.t), 0, 1, this.texturewidth);
    
this.zbuffer[temp.x] = temp.perpwalldist;
  }
    for(
temp.sprite : this.sprites){
      
temp.spi = temp.sprite[0];
      
temp.spx = temp.sprite[1] - this.posx;
      
temp.spy = temp.sprite[2] - this.posy;
      
temp.invdet = 1/(this.planex * this.diry - this.dirx * this.planey);
      
temp.transformx = temp.invdet * (this.diry * temp.spx - this.dirx * temp.spy);
      
temp.transformy = temp.invdet * (-this.planey * temp.spx + this.planex * temp.spy);
      
temp.spritescreenx = int((this.w/2) * (1 + temp.transformx / temp.transformy));
      
temp.spriteheight = abs(int(this.h / temp.transformy));
      
temp.drawstarty = -temp.spriteheight/2 + this.h/2;
      if(
temp.drawstarty < 0){
        
temp.drawstarty = 0;
      }
      
temp.drawendy = temp.spriteheight/2 + this.h/2;
      if(
temp.drawendy >= this.h){
        
temp.drawendy = this.h -1;
      }
      
temp.spritewidth = abs(int(this.h/temp.transformy));
      
temp.drawstartx = -temp.spritewidth/2 + temp.spritescreenx;
      if(
temp.drawstartx < 0){
        
temp.drawstartx = 0;
      }
      
temp.drawendx = temp.spritewidth/2 + temp.spritescreenx;
      if(
temp.drawendx >= this.w){
        
temp.drawendx = this.w -1;
      }
      for(
temp.tx = temp.drawstartx; temp.tx < temp.drawendx; temp.tx ++){
        
temp.texx = int(temp.tx - (-temp.spritewidth/2 + temp.spritescreenx) * 32 / temp.spritewidth);
        
temp.texx = 0;
        
//Buggggs ugh
        
if(temp.transformy > 0 && temp.tx > 0 && temp.tx < this.w && temp.transformy < this.zbuffer[temp.tx]){
          
temp.sh = temp.drawendy - temp.drawstarty;
          
Gunderak_3D.drawimagestretched(temp.tx, temp.drawstarty, 1, temp.sh, temp.spi, temp.texx, 0, 1, 32);
        }
      }
    }
  
temp.ms = 0.1;
  if(
keydown(1)){
    
temp.olddirx = this.dirx;
    
this.dirx = this.dirx * this.ntrig[1] - this.diry * this.ntrig[0];
    
this.diry = temp.olddirx * this.ntrig[0] + this.diry * this.ntrig[1];
    
temp.oldplanex = this.planex;
    
this.planex = this.planex * this.ntrig[1] - this.planey * this.ntrig[0];
    
this.planey = temp.oldplanex * this.ntrig[0] + this.planey * this.ntrig[1];
  }
  if(
keydown(3)){
    
temp.olddirx = this.dirx;
    
this.dirx = this.dirx * this.trig[1] - this.diry * this.trig[0];
    
this.diry = temp.olddirx * this.trig[0] + this.diry * this.trig[1];
    
temp.oldplanex = this.planex;
    
this.planex = this.planex * this.trig[1] - this.planey * this.trig[0];
    
this.planey = temp.oldplanex * this.trig[0] + this.planey * this.trig[1];
  }
  if(
keydown(2)){
    
this.steps += this.smode;
    if(
this.map[int(this.posx - this.dirx * temp.ms*this.wallcheck)][int(this.posy)] == 0){
       
this.posx -= this.dirx * temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy - this.diry * temp.ms*this.wallcheck)] == 0){
      
this.posy -= this.diry * temp.ms;
    }
  }
  if(
keydown(0)){
    
this.steps += this.smode;
    if(
this.map[int(this.posx + this.dirx * temp.ms*this.wallcheck)][int(this.posy)] == 0){
       
this.posx += this.dirx * temp.ms;
    }
    if(
this.map[int(this.posx)][int(this.posy + this.diry * temp.ms*this.wallcheck)] == 0){
      
this.posy += this.diry * temp.ms;
    }
  }
  if(
this.steps > 10){
    
this.smode = -4;
  }
  if(
this.steps < 1){
    
this.smode = 4;
  }
  
temp.so = int(this.steps);
  new 
GuiBitmapCtrl("3D_R_GUN") {
    
x = thiso.off[0] + thiso.w/2 - 35;
    
y = thiso.off[1] + thiso.h - 64;
    
bitmaprectangle = {0,0,81,92 - thiso.steps};
    
bitmap = "gunderak_3d_gun.png";
    
thiso.catchevent(this, "onMouseDown", "onFireGun");
  }
  
settimer(0.05);
}
function 
onFireGun(){
  
//player.chat = "bang!";
} 
Attached Thumbnails
Click image for larger version

Name:	wolftextures.png
Views:	498
Size:	25.2 KB
ID:	55967  
Attached Images
   
__________________

Gund for president.

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