Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Distance from camera

Started by gnitsuk Feb 25, 2008 at 6:27 AM 4 replies 600+ views
Original Post
gnitsuk
gnitsuk
Can anyone help me with this one? I have a sprite rendered in a 3D scene using billboarding. As I move the camera further from the sprite so it shrinks in apparent size. As I move the camera closer so it grows. By calling (Just before I render the sprite): m_device.Transform.World = Matrix.Scaling(x, y, 1.0f); I want to keep the sprite the same apparent size. I've got this working with a quick rule of thumb for my app, I just call: float x,y; GetScalingFactors(out x, out y, new Vector3(/*Sprite's x,y,z position*/)); m_device.Transform.World = Matrix.Scaling(x, y, 1.0f); Where GetScalingFactors is defined as: private void GetScalingFactors(out float x, out float y,Vector3 position) { x = 0.01f; y = 0.01f; Vector3 pointToEye = m_eye - position; float DistanceToEye = pointToEye.Length(); if (DistanceToEye == 0.0) { DistanceToEye = float.MinValue; } x = DistanceToEye / 600.0f; y = DistanceToEye / 600.0f; } Then I just call: m_sprite.Draw(m_texture, new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f/x, 1.0f/y, -1.0f), Color.White.ToArgb()); But obviously this is not exact. Can anyone point my to a formula that will exactly compensate for the sprite to camera distance so the the sprite maintains the same size on screen? Thanks, Mitch.
Evil Steve
Evil Steve
Just change to an orthographics projection before rendering the sprite (The C++ function is D3DXMatrixOrthoOffCenterLH(), not sure about C#). That way you won't get the sprite changing size as it moves to and from the camera.
gnitsuk
gnitsuk
Thanks for your reply,

Without going into the details, for my application this isn't what I need.

I do need to do it the way I've indicated,

Thanks again,
Mitch.
chronozphere
chronozphere
I have tried combining ortho and perspective matrices in one scene and it was problematic. I ran into various problems. You have to deal with the z and w components very carefully.

Check one of my old topics on PGD:

http://www.pascalgamedevelopment.com/viewtopic.php?t=4206&highlight=zbuffer

Hope this helps. :)
Buckeye
Buckeye
First, I'm not familiar with (I think) C# so I'm not sure how everything works in your code.

However, if I understand what you want to do, just decide how high (or wide) you want the object to appear at a particular distance (say 10 units at a distance of 20 units).

If "actual_height" is the actual height of the object when you create it:

float sizeRatio = picked_height/(2*actual_height*picked_distance);

Keep that value around somewhere.

Then, at a new distance:

float scale = sizeRatio*newDistance;

Scale each dimension with the scale number: newX = oldX*scale, etc.

That should get you started.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
gnitsuk
gnitsuk
Thanks for your reply, will give it a try.

Mitch.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.