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 O-R-C · Jul 01, 2016 at 05:39 AM · dllproceduralc++conversiongalaxy

Converting c++ to c# and procedural galaxy generation.

I am currently working on a project were I want to randomly generate a galaxy. Using the information found here I have been able to generate a rather good looking galaxy, however, I think that I could do better.

The website linked above contains source code on generating a galaxy, however, it is made in c++. I have been playing around with different aspects of this code for the last week, however, I have not been able to convert one section of the script into c#.

Bare with me, as the section is composed of a series of sections.

To start with the following line of code is presented in the .h script.

 class CumulativeDistributionFunction
 {
   public:
     typedef double (CumulativeDistributionFunction::*dist_fun_t)(double x);
   private:
     dist_fun_t m_pDistFun;
 }

I did some research into 'typedef' and found that it is used to define a variable. In this case it is showing that m_pDistFun is a double. However, I do not understand what the '*' and the extra (double x) is doing.

Another point of confusion is when the variable for m_pDistFun is set in the cpp script.

 void CumulativeDistributionFunction::SetupRealistic(double I0, double k, double a, double RBulge, double min, double max, int nSteps)
 {
 
 m_pDistFun = &CumulativeDistributionFunction::Intensity;
 
 }

What confuses me most about this is that the function "Intensity" takes a variable like shown bellow.

 double Intensity(double x){
   return somevalue;
 }

I believe that this can be done because of how 'm_pDistFun ' is used in a calculation.

     y += h/3 * ((this->*m_pDistFun)(m_fMin + i*h) + 4*(this->*m_pDistFun)(m_fMin + (i+1)*h) + (this->*m_pDistFun)(m_fMin + (i+2)*h) );

I have no idea what 'this->' does. I think it is a value being put into the 'Intensity' calculation but I am not sure.

I would really appreciate if someone will c++ knowledge could have a look at the source code and explain to me what is happening so I can replicate the affects in c#.

Comment
Add comment · Show 1
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 tanoshimi · Jul 01, 2016 at 05:49 AM 0
Share

It's rather a big ask to explain C++ in a Q&A style site! However, to answer most of your questions I recommend you look up pointers - they're pretty fundamental to the language and will be explained in any introductory C++ reference.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toromano · Jul 01, 2016 at 06:28 AM

dist_fun_t is a function pointer which takes double as a parameter and returns a double. This is how you can assign it to "CumulativeDistributionFunction::Intensity" function since the signature is valid for the case. You can think function pointers as delegates in c#. (Also i should mention that "functors" in c++ are more similar to delegates in c# since they are callable objects too.)

The expression "(this->*m_pDistFun)(m_fMin + i*h)" is how member function pointers are called.

http://www.learncpp.com/cpp-tutorial/78-function-pointers/

"this->" does the same for c++ and c#. "this" is the object itself in the class scope. (Also you might use "this." sometimes instead of of "this->" if the member object is stack allocated.)

http://en.cppreference.com/w/cpp/language/this

Hope this will be useful.

Comment
Add comment · Show 2 · 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 O-R-C · Jul 01, 2016 at 07:31 AM 0
Share

@toromano That sounds about right. So what you are saying is that calling

 (this->*m_pDistFun)(m_f$$anonymous$$in + i*h)

is the same as calling

 Intensity(m_f$$anonymous$$in + i*h)

however, due to the nature of c++ it was written like the former?

avatar image toromano O-R-C · Jul 01, 2016 at 08:15 AM 0
Share

It is not about c++ being an older language. There is probably a good reason to use function pointers rather than calling the functions directly in the sdk that you are using. Function pointers let you use functions as parameters and be able to pass them to functions. It is more flexible.

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

47 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

Related Questions

How to step into a native C++ dll in Visual Studio? 0 Answers

DllNotFoundException in standalone 2 Answers

RunTime Error in Unity when I'm using a function from a C++ Dll 0 Answers

proceduraly generated galaxy 3 Answers

C++/CLI DLL import error 0 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