Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP / GDK] Angular size of an object in PIXELS - small angle formula

Author
Message
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 12th Feb 2010 14:06 Edited at: 12th Feb 2010 14:07
Hi all,

This code calculates the size in pixels of an object in the screen, using the "small angle approximation" formula.

It works for any camera Field Of View angle (FOV).
Ideal for Level Of Detail (LOD) systems based on size in pixels in screen (rather than based directly on distance calculus).

It allows to precisely find out if an object is not visible anymore (pixel size < 1), or when it has a particular size on the screen.
Particularly useful for LOD binocular view, or any other changing FOV scenery.

Also suitable for many other uses, as line of sight occlusion, combined with the x,y screen coordinates:
Knowing the 2d screen coordinates, the size in pixels, and the distance, it can be known if an object is visually occluded by another one.

Note on accuracy: This code is based on the "small angle approximation" formula, a simplification of the laws of trigonometry which only returns an approximated value. The greater the angle measured, or the FOV angle, the greater the error in the returned value.
Despite of this, accuracy should suffice for a standard gaming environment.

DBPro code version:


DGDK code version:


The maths behind the size in pixels is very simple using the small angle formula, just a couple of multiplications, so don't be afraid to use it

A note on the DBPro version, I have noticed that DBPro's screen resolution includes the window menu, so the pixel ratio looks weird here. To correct this, fix the camera aspect by adding the menu size (8x34 pixels), for example 640x480 would be:

Not a thing to worry if using the right full screen resolution.

DGDK image:



If you use any portion of this code in a program, please credit "MPL3D", I got a brand name to maintain

Attachments

Login to view attachments
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 22nd Apr 2010 14:39 Edited at: 22nd Apr 2010 18:38
This is an example application of the pixel size algorithm, a basic custom texture LOD system.

The code calculates the pixel size of a plane and then textures the object with the corresponding texture size.
In the example these are 800x600, 400x300, 200x150 and 100x75. Of course this is just the basic idea, and ideally it should be adapted to your own needs.

This is the DGDK code version, but it is quite simple to translate it to DBPro (no C++ features used):



Also attached the DGDK project with a compiled executable.

I will keep on posting some more applications of the pixel size method here, hope you find them useful.

Attachments

Login to view attachments
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Apr 2010 15:27
Doesn't this just estimate the size of the object based on diameter and distance from camera though? What if your object is long and thin, or square?

I was hoping it would say how many actual pixels of your object are visible...

Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 29th Apr 2010 14:02 Edited at: 29th Apr 2010 14:04
You are right, this is ideal for square or round objects, that have equal x/y/z dimensions.

If the object is, for example, 'long and thin', then I recommend to use the largest, the shortest, or the average size to calculate the pixel size, it depends on each particular case.

Quote: "I was hoping it would say how many actual pixels of your object are visible... "

Well, this is a fast calculus. It doesn't perform the integration of complex shapes.

However, the pixel size gives us a criteria to decide the LOD level.
Take for example the DBPro LOD command:

ADD LOD TO OBJECT Object Number, LOD Object, LOD Level, LOD Distance

Now, what should be that 'LOD distance'? 10 units? 100 units? 1000?
With the pixel size you can calculate at which distance the object will have xx pixels in the screen.
So instead of figuring out the distance, you can set it, for example, to a distance when the object is 100 pixels wide.
If the object is 'long and thin', well, this was a problem already with the DBPro function. As said, this calculus is just fast, not accurate.

This is a DBPro function that calculates at which distance an object will have a certain pixel size:



It should be used to determine the distance parameter for the 'ADD LOD TO OBJECT' DBPro function.

Login to post a reply

Server time is: 2024-04-20 05:03:20
Your offset time is: 2024-04-20 05:03:20