Author Topic: Floor Casting  (Read 13413 times)

0 Members and 1 Guest are viewing this topic.

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Floor Casting
« on: August 02, 2010 »
Been struggling with the following (I think I found the idea on here originally)
Want to always have a single value on each line (you can see where the value is anded) but it's not always a straight line. If you uncomment the  ' camX += 0.1 line, you can see it flickering when moving.

Been struggling with this for a bit, so would be very happy if someone can solve this problem for me!!

cheers


Code: [Select]
' Floor Casting
const XRES = 640
const YRES = 480
'const xres = 320
'const yres = 240

#include once "tinyptc.bi"
if (ptc_open("FLOOR CASTING", 640, 480) = 0) then end -1

dim shared as integer buffer(XRES*YRES)
Dim Shared as single u, v, w, camX, camY

 Declare Sub floor()

while (inkey$ = "")
  floor()

  ptc_update@buffer(0)
      ERASE BUFFER

Wend
ptc_close
end 0

Sub floor()
dim as integer c, b, r

  'camY += 1
'  camX += 1 'xres*sin(camY*3.14159/180)
 ' camX += 0.1
  b = 150*XRES
 
  For y = 50 to 200-1
 
    w  = -240 / y
   
    v =  XRES*w - camY
   
    u = -320*w + camX
   
     for x = 1 To XRES-1
r=(u And 255)
Select Case r
Case 0
buffer(b+x) = rgb(255,0,0)
Case Else
buffer(b+x) = rgb(0,0,0)
End Select
'       Else
' buffer(b+x) = rgb(0,0,0)

       'buffer(b+x) = rgb(u And 255,v And 255,0)
       u=u+w
     Next x
   
    b=b+XRES
  Next y

End Sub

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: Floor Casting
« Reply #1 on: August 02, 2010 »
maybe it's because of mixing floats with integers.
should this look like a chessboard?

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #2 on: August 03, 2010 »
Yes, I've just tried to isolate 1 position on the x axis

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #3 on: August 03, 2010 »
So your problem are the missing pixels in the lines?
That's because interpolation skips some values (you sometimes just don't catch the "0"-case).
And points in consecutive rows can be further apart than a single pixel (so there will be gaps, too).
After all your algorithm isn't very suitable to draw continuous lines.

Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #4 on: August 03, 2010 »
I thought as much.
So what would be the best / easiest way to calculate true "parallax" points?

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #5 on: August 03, 2010 »
So what would be the best / easiest way to calculate true "parallax" points?
Your "points" are ok, they just don't make a continuous line.
To get lines just calculate the x-position of each line in the top row (just skip the black area between two pixels) and the corresponding x-position in the bottom row (which is just scaled at the center of the screen).
Then draw the line using Bresenham or Wu.
Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #6 on: August 03, 2010 »
Thanks for the links.
Trouble is, there isn't always a point to reference. I guess you mean abandon this idea and do it with 2 sets of points; one distant and one close up, draw the lines and look up the set value on each line?

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #7 on: August 03, 2010 »
Actually, scratch that.
I just tried that out, but in my dimness, still am not able to work out how far apart they should be etc.

I guess I'm just asking for someone to show me :)

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #8 on: August 03, 2010 »
I am not able to work out how far apart they should be etc.
What exactly is the problem?
Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #9 on: August 03, 2010 »
Essentially how to calculate flawless parallax effect. Like my original post, but one that actually works.

I guess it's just easier to show you what I`m trying to do... this example still flickers, even with allowing 0-3 as a (masked) value

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #10 on: August 04, 2010 »
how to calculate flawless parallax effect.
"Parallax" means you're moving multiple parallel layers according to their depth - that's exactly what you aready do with every scanline and in fact there's nothing wrong with it.
But if you want lines it just won't work. You can have a look at the case where bresenham distinguishes horizontal and vertical lines to see why.
To get proper lines you need a different approach. But I'm still a little confused what exactly you're trying to achieve as your example shows bars, too...  ???

The easiest way to get continuous lines is to keep track of the positions in the previous scanline and fill the horizontal space between two contiguous points.
Two points in different rows belong to the same line when their "u" have the same "integer part" ( u shr 8 ).
Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #11 on: August 04, 2010 »
The bars are what I'm actually trying for.
The points are just a representation of how the bars are positioned (bar positioned at point on X axis, each layer is on Y axis)
However, i can't trust a specific "point" as it's not always set.
Essentially, I want to know the gap between each point/bar on each line and as I can't be sure where a "point" is from one frame to the next, there's my problem

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #12 on: August 04, 2010 »
Now I'm getting it! :D
All you want to know is whether "r" crossed zero while stepping to the next pixel.
Just test if the current "r" is smaller than the "r" from the previous pixel (you can simply store it in another variable).

Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #13 on: August 04, 2010 »
Sorry to sound thick, but can you demonstrate on that first piece of code :)

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Floor Casting
« Reply #14 on: August 04, 2010 »
This probably it's not what you want Nzo, but it might be useful for something else.

I just changed this line:
Code: [Select]
r=(u And 255)to
Code: [Select]
r=(u And 256)
Code: [Select]
#define PTC_WIN

' Floor Casting
const XRES = 640
const YRES = 480
'const xres = 320
'const yres = 240

#include once "tinyptc.bi"
if (ptc_open("FLOOR CASTING", 640, 480) = 0) then end -1

dim shared as integer buffer(XRES*YRES)
Dim Shared as single u, v, w, camX, camY

 Declare Sub floor()

while (inkey$ = "")
  floor()

  ptc_update@buffer(0)
      ERASE BUFFER

Wend
ptc_close
end 0

Sub floor()
dim as integer c, b, r, x, y

  'camY += 1
  camX += 1   'xres*sin(camY*3.14159/180)
 ' camX += 0.1
  b = 150*XRES
 
  For y = 50 to 200-1
     
    w  = -240 / y
   
    v =  XRES*w - camY
   
    u = -320*w + camX
   
     for x = 1 To XRES-1   
     
      r=(u And 256)
         
      Select Case r
         Case 0
          buffer(b+x) = cast(integer,rgb(255,0,0))
         Case Else
          buffer(b+x) = cast(integer,rgb(0,0,120))
      End Select

      u=u+w
             
     Next x
   
    b=b+XRES
  Next y
   
End Sub
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #15 on: August 05, 2010 »
Sorry to sound thick, but can you demonstrate on that first piece of code :)
Sure.
This way you won't miss "r=0" when r jumps from eg 254 to 1:
Code: [Select]
Sub floor()
  dim as integer c, b, r, x, y, prer
  camX += 0.1
  b = 150*XRES
 
  For y = 50 to 200-1
    w  = 240 / y
    v =  XRES*w - camY
    u = -320*w + camX
    r= 0
   
    for x = 0 To XRES-1
      prer= r   ' r from previous pixel
      r=(u And 255)
     
      if (r < prer) then
          buffer(b+x) = 255 shl 16
      else
          buffer(b+x) = 0
      end if
     
      u=u+w
    Next x
   
    b=b+XRES
  Next y
   
End Sub
Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #16 on: August 05, 2010 »
Riiiiight :)
Now I see what you mean... duh
Splendid.. thanks a lot for putting up with my stupidness!

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Floor Casting
« Reply #17 on: August 05, 2010 »
Quote
thanks a lot for putting up with my stupidness!
No problem!
You're now testing every single pixel if it's the one you're looking for - that's not a very elegant algorithm.
You already know that you want every 256th pixel at a step-size of "w".
So after you've found the first pixel, the next one will be "256/w" pixels away.
Challenge Trophies Won:

Offline nzo

  • Atari ST
  • ***
  • Posts: 126
  • Karma: 68
    • View Profile
    • Amiga Remakes in DHTML
Re: Floor Casting
« Reply #18 on: August 05, 2010 »
Even better !

Now you can see where this is going :)

K++

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Floor Casting
« Reply #19 on: August 05, 2010 »
Yeah ... copperbar - madness!

Good co-op work, mates!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won: