No a raycast is much different than that.

Raycast will divide the ray into itx x and y components.

Essentially in ortho ray casting if you know the size of each cell then you can find out the stepsize for each component via tangent instead of sin cos.

cos(theta)=adj/hyp

sin(theta)=opp/hyp

tan(theta)=opp/adj

if you know adj or opp which we do...its the cellsize, then you can find the other.

This is essentially a projection transform...you are finding the height of the wall by dividing by the distance from the camera, but to equate raycasting with an actual projection transform is not a good habit. They are two fundamentally different approaches. One is fake 3D and one is true 3D.

Actually in DX there is more going on than that. The clip matrix transforms from camera space to clip space to get the correct value in W.

Direct3D clip matrix:

0<=z<=w

**M**clip=

[zoomx 0 0 0]

[0 zoomy 0 0]

[0 0 (f/(f-n) 0]

[0 0 (nf/(n-f)) 0]

then:

screenx=(clipx*resx/2*clipw)+screencenterx;

screeny=(clipy*resy/2*clipw)+screencentery;

zclip and wclip can be used for zbuffering and perspective correction.

But I think we are straying off topic.

Here is a crappy slow raycast using sin cos:

Code:

void Slow2DRaycast(Player &thePlayer)
{
//Compute casting angle for 60 degree FOV
float angle=thePlayer.angle-30f;
if (angle<0.0f) angle+=360.0f;
//Compute angle increment for screen res x
float angleinc=60.0f/horiz_res_x;
//Ray position
float rayx=0.0f,rayy=0.0f;
//Ray steps
float ix=0.0f,iy=0.0f;
//Height of hit wall
float heightofslice=0.0f;
//Map position intersected by ray
int mapx=0,mapy=0;
//Color of slice
int wallcolor=0;
//Floor and ceiling screen y locations
int floor=0;
int ceiling=0;
//Cast from 0 to horizontal screen res
for (int screenx=0;screenx<horiz_res_x;screenx++)
{
//Set ray origin at player location
rayx=thePlayer.x;
rayy=thePlayer.y;
//Compute increments
ix=cos(DEGTORAD(angle));
iy=sin(DEGTORAD(angle));
do
{
//Advance ray
rayx+=ix;
rayy+=iy;
//Compute ray map coords from ray world coords
int mapx= rayx/CELLSIZE;
int mapy=rayy/CELLSIZE;
//keep ray on map - only works on maps that are a power of 2
mapx&=(mapwidth-1);
mapy&=(mapheight-1);
//Retrieve value from map at ray intersection
wallcolor=map(mapy*mapwidth+mapx);
//Check to see if we hit a wall
if (wallcolor)
{
float dx=rayx-thePlayer.x;
float dy=rayy-thePlayer.y;
//Compute actual distance from player
//extremely slow - could use cos for distance
heightofslice=sqrt((dx*dx)+(dy*dy));
//Compute ceiling and floor
ceiling=centerscreeny-(heightofslice/2.0f);
floor=ceiling+heightofslice;
//Draw the slice
VerticalLine(screenx,ceiling,floor,wallcolor);
}
} while (wallcolor==0);
//Increment angle and check for angle>360.0f
angle+=angleinc;
if (angle>360.0f) angle-=360.0f;
}
}