Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jwvanderbeck · Dec 31, 2016 at 05:42 AM · scalemathcalculations

Adjust scale of object based on distance to maintain same perceived size

Hi all,

Having a little trouble explaining what I am trying to do so please bear with me. I am working on a demo that involves massive scales in the space. To make things more manageable I want to do is keep the planets closer together in game units, but scale them so that they appear to be the real world size as seen from that location.

For example let's say the camera is at the Earth. Instead of putting the sun 150 million kilometers away and making it have a radius of 695,700km, I want to put it say just 10,000 meters away from the Earth but scale it down (some unknown amount) so that the PERCEIVED size would be the same.

However despite an hour or so of Googling I can't seem to figure out how to calculate the scale factor based on distance. I ran into some topics about perceived angle, but I don't understand what that means, nor how I could even translate an "angle" into a size in game.

I feel like I am close to the answer, spiraling in on it but not quite hitting it. Here is the code I am currently using. This is in C#

 Vector3d activeRealPosition = activeBody.GetOrbitPosition("real");
 Vector3d activeGamePosition = activeBody.GetOrbitPosition("play");
 Vector3 realOrbitPosition = Vector3.zero;
 Vector3 actualOrbitPosition = Vector3.zero;
 if (TypeOfBody != BodyType.Star)
 {
     realOrbitPosition = transform.TransformPoint(_orbits["real"].position); // CelestialOrbit stores position in local space
     actualOrbitPosition = transform.TransformPoint(_orbits["play"].position);
 }
 double realDistance = Vector3d.Distance(activeRealPosition, realOrbitPosition);
 double actualDistance = Vector3d.Distance(activeGamePosition, actualOrbitPosition);
 
 Debug.Log("Real Distance: " + realDistance + ", Actual Distance: " + actualDistance);
 // actual distance should always be much smaller than real distance
 if (actualDistance > realDistance)
     Debug.LogWarning("[UpdateScale] Actual distance is larger than real distance");
 double difference = (realDistance - (realDistance-actualDistance)) / (realDistance);
 Debug.Log("Difference: " + difference);
 double newScale = RealRadius * difference;
 transform.localScale = Vector3.one * (float)newScale;

I am basing it on some manual tests I did just positioning two objects at different distances and looking if they appear the same size, and in those manual setups they did. So I then tried to apply that math to the real deal, but it doesn't work. The math is based on: http://www.calculatorsoup.com/calculators/algebra/percent-change-calculator.php

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tomekkie2 · Dec 31, 2016 at 08:14 AM 0
Share

Deter$$anonymous$$e your planet's size on the screen:

 float getScreenSize () 
 {
 transform.LookAt(Camera.main.transform, transform.up);
 Bounds bounds = gameObject.GetComponent().bounds;
 Vector3 v1 = Camera.main.WorldToScreenPoint(transform.position - 0.5f * bounds.size.y * transform.up);
 Vector3 v2 = Camera.main.WorldToScreenPoint(transform.position + 0.5f * bounds.size.y * transform.up);
 return Vector3.Distance(v1, v2);
 }

I have assumed the shape of your planets is spherical. Note that the results of the above will not be reliable with the planets screen size is around or exceeds the screen height.

Then rescale the object to the desired size by stetting transform.localScale.

avatar image jwvanderbeck tomekkie2 · Dec 31, 2016 at 06:02 PM 0
Share

Interesting take on the problem, but I am not sure using the camera to deter$$anonymous$$e screen size would work as the camera isn't always seeing these objects. Basically body that isn't the active planet needs to be scaled down, in case the player does move the camera in such a way as to see them.

In any case, I finally found my problem with fresh eyes this morning. I should not have been transfor$$anonymous$$g the orbital values from Local to World space. Once I changed that it started working well.

avatar image Glurth jwvanderbeck · Dec 31, 2016 at 11:53 PM 0
Share

oops- missed this. oh well.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Dec 31, 2016 at 11:49 PM

Note: I have not actually tested this out. Please let me know if it fails, so I can remove this post.

I think you can use the law of similar triangles to figure this out.

Givens:

 SunActualRadius, SunActualDistanceFromCamera, SunGameUnitDistanceFromCamera

Desired output:

 SunGameUnitRadius

Make a right triangle using SunActualRadius as side A, and SunActualDistanceFromCamera as side B. The angle to the hypotenuse defines how “big” the sun looks from the camera position. How we use this angle, is simply by duplicating it: Draw an identical right triangle, with the same angles. Now, label side A of this new triangle SunGameUnitRadius and side B SunGameUnitsFromCamera. Since these two triangles are identical, the ratio of sides A and B will also be identical. Expressed as a formula:

 SunActualRadius/ SunActualDistanceFromCamera = SunGameUnitRadius/ SunGameUnitDistanceFromCamera

Solve for our desired output:

 SunGameUnitRadius = SunActualRadius * SunGameUnitDistanceFromCamera / SunActualDistanceFromCamera
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by elenzil · Jan 01, 2017 at 05:19 AM

i think you're making this too hard.

just scale everything down by some constant factor.

in this case your scale factor would be 10,000 meters divided by 150,000,000,000 meters. so just multiply 695,700,000 by (10,000 / 150,000,000,000) to get the scaled radius.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the correct ratio of collision distance to total scale? 1 Answer

Sprite doesn't flip correctly. 1 Answer

How to simplify the equation of a moving vertex if its object is squeezed? 0 Answers

Calculate launch angle based on launch direction 2 Answers

Very big numbers (size, mass, etc), Scientific Notation 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges