Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Supershandy · Nov 09, 2013 at 09:55 AM · javac++conversionratio

Conversion of a C++ variable to UnityScript?

As part of my own learning course I am looking at building solar system models using Unity as a platform, I know that this can be possible with the "Universe Sandbox" being developed in Unity.

I came across a C++ code that I'm using for reference, however I came across a bit of an unknown in a variable.

static const fixed SUN_MASS_TO_EARTH_MASS = fixed(332998,1);

Doing a little investigation I found that obviously "static" is a norm in programming and the "SUN_MASS_TO_EARTH_MASS" is just the variable name. However "fixed" and "const" are throwing me a little bit as was "fixed(332998, 1), but that bit I figured out to be a ratio as the ratio of the sun to the earth is roughly 333000 : 1.

So my question is, how can I set that ratio in Unity as it is used elsewhere in the code as part of a formula, and also what is "fixed" and "const" and can they be replicated in unity?

Many Thanks

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by VioKyma · Nov 09, 2013 at 10:55 AM

const is a constant variable in C++, meaning it cannot be changed (assigned to) and will remain the same at all times during the code's execution.

fixed is the variable type. This simply means it is represented in fixed point notation. This is done for performance, or in case the device cannot perform floating point operations.

In short: You can just use this as a regular int or float as required. Anybody with a device capable of running Unity can do floating point calculations.

const var SUN_MASS_TO_EARTH_MASS = 332998;

Add private/public/static as needed (I'm not sure on the exact context of your script).

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
2

Answer by whydoidoit · Nov 09, 2013 at 10:58 AM

Well "fixed" means "fixed point" as opposed to "floating point" - i.e. the number has a fixed number of decimal digits (1 in this case).

Const means it's a value not a variable and cannot be changed by the programme - which allows certain compiler optimisations.

Off hand in UnityScript I don't know if you can declare a const, but I wouldn't worry about it. You could use a System.Decimal to hold a fixed point number, but there's no fiddling with the number of digits with a decimal - I wouldn't bother and just define it as:

  static var SUN_MASS_TO_EARTH_MASS = 332998.0;

Perhaps if calculations get very big you could declare it as a double:

  static var SUN_MASS_TO_EARTH_MASS : double = 332998;

Doubles use twice the memory and are hence more accurate than floats - but you will end up having to cast things back to float to use in other parts of Unity.

Comment
Add comment · Show 5 · 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 Supershandy · Nov 09, 2013 at 11:19 AM 0
Share

Ok, that explains how to set the ratio bit and all the other parts, the only other thing is that some ratio values are not always "something" to "1", there is another fixed variable that has the values (3, 65355). How would i go about settibg that as a ratio?

avatar image VioKyma · Nov 09, 2013 at 11:21 AM 1
Share

A ratio is, by definition, the result of dividing two numbers. Therefore, just do that division and store the result in a double

avatar image whydoidoit · Nov 09, 2013 at 11:24 AM 1
Share

I doubt that this is a definition of a ratio - the second parameter would appear to indicate how precise the number was going to be. Just ignore the second parameter when you are creating the variables.

avatar image VioKyma · Nov 09, 2013 at 11:35 AM 1
Share

Right you are. It looks more like a fixed value to x decimal places.

avatar image Supershandy · Nov 09, 2013 at 11:40 AM 0
Share

Cheers guys for the input, has been quite valuable!

At least i can understand what is going on a bit better now in the code :)

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

18 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

Related Questions

A node in a childnode? 1 Answer

Swipe in Unity3d 3 Answers

Android - Trying to Load Library bug 0 Answers

[SOLVED] Java Converters In Unity? 2 Answers

How would you convert ogg files to wav files properly using C# scripting? 1 Answer


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