Graal Forums

Graal Forums (http://forums.graalonline.com/forums/index.php)
-   Code Gallery (http://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   GS2 3D Raycaster (http://forums.graalonline.com/forums/showthread.php?t=134270428)

Gunderak 11-23-2016 10:40 AM

GS2 3D Raycaster
 
4 Attachment(s)
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.

https://embed.gyazo.com/5a4818b46b41...d7759cc4ea.gif

I had some floor rendering, but it was extremely laggy as I couldn't access the image buffer directly.
https://embed.gyazo.com/e88d25d64e52...c087d196b5.png

Graal Script 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"13},
    {
"door.png"31},
  };
  
this.texturewidth 64;
  
this.700;
  
this.zbuffer = new[this.w];
  
this.500;
  
this.off = {screenwidth/this.w/2screenheight/this.h/2};
  
this.off = {7070};
  
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;
    
thiso.off[0];
    
thiso.off[1];
    
clearall();
  }
  
onTimeout();
}
function 
sqrt(n) { 
  return 
n^0.5
}
function 
onTimeout(){
  
Gunderak_3D.drawimagestretched(00this.wthis.h/2"gunderak_sky.png"0011);
  
Gunderak_3D.drawimagestretched(0this.h/2this.wthis.h/2"gunderak_floor.png"0011);
  for(
temp.0temp.this.wtemp.++){
    
temp.camerax temp.x/this.-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(+ (temp.raydiry temp.raydiry)/(temp.raydirx temp.raydirx));
    
temp.deltadisty sqrt(+ (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 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 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 + (temp.stepx)/2)/temp.raydirx);
    }else{
      
temp.perpwalldist abs((temp.mapy temp.rayposy + (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.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 == && temp.raydirx 0){
      
temp.texx this.texturewidth temp.texx 1;
    }
    if(
temp.side == && temp.raydiry 0){
      
temp.texx this.texturewidth temp.texx 1;
    }
    
temp.texx this.texturewidth temp.texx 1;
    
temp.= {111};
    if(
temp.side == 1){
      
temp.= {0.650.650.65};
    }
    
temp.temp.drawend temp.drawstart;
    
Gunderak_3D.drawimagestretched(temp.xtemp.drawstart1temp.h"wolftextures.png"temp.texx+(thiso.texturewidth*temp.t), 01this.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) * (temp.transformx temp.transformy));
      
temp.spriteheight abs(int(this.temp.transformy));
      
temp.drawstarty = -temp.spriteheight/this.h/2;
      if(
temp.drawstarty 0){
        
temp.drawstarty 0;
      }
      
temp.drawendy temp.spriteheight/this.h/2;
      if(
temp.drawendy >= this.h){
        
temp.drawendy this.-1;
      }
      
temp.spritewidth abs(int(this.h/temp.transformy));
      
temp.drawstartx = -temp.spritewidth/temp.spritescreenx;
      if(
temp.drawstartx 0){
        
temp.drawstartx 0;
      }
      
temp.drawendx temp.spritewidth/temp.spritescreenx;
      if(
temp.drawendx >= this.w){
        
temp.drawendx this.-1;
      }
      for(
temp.tx temp.drawstartxtemp.tx temp.drawendxtemp.tx ++){
        
temp.texx int(temp.tx - (-temp.spritewidth/temp.spritescreenx) * 32 temp.spritewidth);
        
temp.texx 0;
        
//Buggggs ugh
        
if(temp.transformy && temp.tx && temp.tx this.&& temp.transformy this.zbuffer[temp.tx]){
          
temp.sh temp.drawendy temp.drawstarty;
          
Gunderak_3D.drawimagestretched(temp.txtemp.drawstarty1temp.shtemp.spitemp.texx0132);
        }
      }
    }
  
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") {
    
thiso.off[0] + thiso.w/35;
    
thiso.off[1] + thiso.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!";



Gunderak 11-23-2016 10:42 AM

Hit the character limit on that post, I used this tutorial: http://lodev.org/cgtutor/raycasting.html
It was written in C++, and I ported it to GS2.

There are two attachments named gunderak_sky.png and gunderak_floor.png, they are just 1x1 images, blue and gray respectively.
Best of luck clicking them.

My desire is to have people learn from this, and possibly build some minigames in GS2! As I don't have the time anymore to do this.

xXziroXx 11-23-2016 01:06 PM

Nice work, but a few things that would optimize it and probably make it run a lot smoother:

- You don't seem to be clearing the drawing panel each cycle? Means you're drawing an insane amount of stuff in the drawing panel that should drop fps more and more the longer you run it.

- Instead of redrawing the world every 0.05 cycle, check if the player has moved or tilted his viewpoint and only redraw the world if required. Would increase fps by loads!


I bet if you did both of the above, drawing floors wouldn't be that laggy either.

Crow 11-23-2016 07:19 PM

Quote:

Originally Posted by xXziroXx (Post 1740409)
- You don't seem to be clearing the drawing panel each cycle? Means you're drawing an insane amount of stuff in the drawing panel that should drop fps more and more the longer you run it.

Isn't the drawing panel simply a buffer? If it is, not clearing it is the preferred method, as clearing a buffer is costly. Since you're filling it entirely again anyway, you don't have to clear it.

xXziroXx 11-23-2016 07:38 PM

Quote:

Originally Posted by Crow (Post 1740411)
Isn't the drawing panel simply a buffer?

I have no idea to be honest.

Crono 11-23-2016 11:32 PM

doom graal? :D

PlanetOscar 11-23-2016 11:49 PM

next rush b cyka blyat game?

Gunderak 11-24-2016 01:29 PM

Quote:

Originally Posted by Crono (Post 1740415)
doom graal? :D

If I had more spare time maybe!
This is the second time you've mentioned that idea haha

Crono 11-24-2016 02:32 PM

ok graalenstein 3d? :D

Kamaeru 11-24-2016 06:05 PM

It's really a shame Graal's frame rate is capped at 20fps.

For comparison many Atari, NES, SNES games, etc run at 60fps.

Crono 11-24-2016 09:28 PM

Quote:

Originally Posted by Kamaeru (Post 1740421)
It's really a shame Graal's frame rate is capped at 20fps.

For comparison many Atari, NES, SNES games, etc run at 60fps.

so do new consol- oh wait they dont. :(

LimetasticSlushy 11-25-2016 07:38 AM

Quote:

Originally Posted by PlanetOscar (Post 1740416)
next rush b cyka blyat game?

If someone could figure out how to make Counter-Strike in Graal, I'd certainly be impressed.

PlanetOscar 11-25-2016 10:43 AM

Quote:

Originally Posted by Kamaeru (Post 1740421)
It's really a shame Graal's frame rate is capped at 20fps.

For comparison many Atari, NES, SNES games, etc run at 60fps.

Alpho was (and maybe still is) working on something that would enable him to do something in 60 fps :D

Kamaeru 11-25-2016 05:17 PM

Quote:

Originally Posted by PlanetOscar (Post 1740425)
Alpho was (and maybe still is) working on something that would enable him to do something in 60 fps :D

What's even worse is that npc-server seems to only operate at 10fps. lol

Crono 11-25-2016 06:15 PM

Quote:

Originally Posted by Kamaeru (Post 1740426)
What's even worse is that npc-server seems to only operate at 10fps. lol

the other 10 is reserved for flying technology :pluffy:


All times are GMT +2. The time now is 10:53 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.