Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Sprites in Raycasting

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Sprites in Raycasting
Billybob
Member #3,136
January 2003

What in the world has my thread become! I'm dumb!. Uhhh, stop dis-ing OpenGL, cause that is what I'm using for my next "3D" project >8)

First off, thanks for all the help, I think what I thought was correct and what you are telling me is making me think that what I thought about being correct in thinking what I thought was good. Get that? Got that? Good.

My engine is not like Doom or Wolfenstien, it is an Advanced Raycaster much like the rex3D engine, but still a raycaster, just slightly different.

I guess I'm off to go work up some code for rendering sprites :) Thanks for the help.

-------------NEW POST-------------------------

Let me run this math by ya and make sure its good.

X is the offset of the sprite's center from the center of the screen.
P is distance to projection plane.
a is Angle between camera and sprite relative to the camera's viewing angle.

X = P * tan a

and of course I can get the height scale of the sprite via my translation function used on the walls and I can use the ratio of the translated height and the real height to figure out how wide to draw the sprite.

All that sound OK? It better be. 8-)

Thomas Harte
Member #33
April 2000
avatar

Quoted code:

odistance=(int)(DIV_VIEW/sqrt((obj_x<i>-px)*(obj_x<i>-px)+(obj_y<i>-py)*(obj_y<i>-py)));
oangle=RAD_TO_DEG(atan2(obj_y<i>-py,obj_x<i>-px))-pa;
if(oangle<0) oangle+=359;
if(oangle>359) oangle-=359;
odisplayx=(int)(oangle/0.1875)+(SCREEN_W/2);
stretch_sprite(front,object[0],odisplayx-odistance/2,100-odistance/2,odistance,odistance);

I have various observations. First of all, I think you actually want this:

if(oangle<0) oangle+=360;
if(oangle>359) oangle-=360;

e.g. the angle '360' is not the same as the angle '1'.

odistance=(int)(DIV_VIEW/sqrt((obj_x<i>-px)*(obj_x<i>-px)+(obj_y<i>-py)*(obj_y<i>-py)));
oangle=RAD_TO_DEG(atan2(obj_y<i>-py,obj_x<i>-px))-pa;
if(oangle<0) oangle+=360;
if(oangle>359) oangle-=360;

So, odistance is the perspective projected size of a square tile in pixels, oangle is the angle between the centre of the view and the object.

odisplayx=(int)(oangle/0.1875)+(SCREEN_W/2);

You've picked oangle to be always positive, so your objects are always on the right hand side of the screen here. It strikes me that you might instead want the following:

odistance=(int)(DIV_VIEW/sqrt((obj_x<i>-px)*(obj_x<i>-px)+(obj_y<i>-py)*(obj_y<i>-py)));
oangle=RAD_TO_DEG(atan2(obj_y<i>-py,obj_x<i>-px))-pa;
while(oangle<-180) oangle+=360;
while(oangle>=180) oangle-=360;
odisplayx=(int)(oangle/0.1875)+(SCREEN_W/2);
stretch_sprite(front,object[0],odisplayx-odistance/2,100-odistance/2,odistance,odistance);

i.e. you restrict your angle to the range [-180,179] rather than [0, 359]. You might be able to use 'if' rather than 'while', I'll let you think about that one!

Quote:

Let me run this math by ya and make sure its good.

X is the offset of the sprite's center from the center of the screen.
P is distance to projection plane.
a is Angle between camera and sprite relative to the camera's viewing angle.

X = P * tan a

I'm confused by P. If you've calculated distance from viewer to object and called this P then in your right angled triangle, you have one angle, and the hypotenuse and want to get the side opposite the angle, so you 'sin angle = opposite / hypotenuse'. Conversely if you want the side adjoining you use cos.

If you have the adjoining side, which I guess you mean is P, then yeah:

tan A = X / P
=> P * tan A = X

Although I reiterate my claim from earlier in this thread that adding objects in a trigonometric manner is silly and processor expensive.

aybabtu
Member #2,891
November 2002

Yay!;D I attached a screenshot...
Thank's Thomas!

X-G
Member #856
December 2000
avatar

Do you have to keep attaching .pcx files? You can't view them inline. It's annoying. At least make them .bmp ... :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

BMP is too big. PNG/GIF is teh win!!

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Thomas Fjellstrom
Member #476
June 2000
avatar

Use png's ;) much better on the bandwidth.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

X-G
Member #856
December 2000
avatar

I said at least, since he's likely generating these screenshots via Allegro, which can't save gif/png without extra libs.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

Yeah, but everyone should have a program for converting BMP's to a multitude of file formats ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

X-G
Member #856
December 2000
avatar

In case he doesn't, I just want to express that even .bmp is better than .pcx, since .bmp can be viewed inline.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

aybabtu
Member #2,891
November 2002

I do have a program, but it's 'new' and takes over a minute to load up...it's so much easier for me (:P) to do it this way...but, from now on...I'll convert them to GIFs.:)

Billybob
Member #3,136
January 2003

ewwww, CODE, I'm dumb!. Theories for me. I eat theories for breakfast, and sometimes for lunch. Well, I'm going to make my entity engine, and then I'll work on the sprite rendering code. If I run into problems with my theories then I'll check out code, heh.

EDIT
BTW, here is my theory. Use the variables as described in past post.

http://www.cljy.com/~billy/Sprite Theory.JPG

 1   2 


Go to: