Search Unity

Newbie question - Using C++ with unity

Discussion in 'Scripting' started by kshaaban, Feb 11, 2011.

  1. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    Hey all,

    So I am a little confused about something and was hoping you could help me to clear things up a little. I have just started to learn unity and was wondering what would be the most appropriate language for me to start learning for my purposes.

    Alot of my current project has already been written up in c++ and i have functions which are producing results (in the form of Co-ordinates) that i would like to "power objects in unity".

    My main reasons for using unity are because i couldnt get my head around using OpenGL (poor excuse i know), so I'm hoping i can reach the same result with this.

    given that half my code is already written in c++ what would the best way of continually feeding this data into unity?

    its a bit of an open question i know so fire away with any questions so i can help clarify.

    Have already had a very warm welcome.

    Thanks
    Khaled :)
     
  2. Germy76

    Germy76

    Joined:
    Dec 1, 2010
    Posts:
    31
    Hello,

    I'm assuming rewriting it in C# is not what you want to do. What you could do is take the C++ solution compile it as a .dll file and use DllImport() to grab those functions from the C++ program, the only problem with this is if you are using the free version, C++ .dll's are only supported in Unity Pro, if I remember correctly.

    This solution will allow you to keep most of you functions although it would still probably still take quite a bit of rewriting.

    Hope that helps some!
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The honest answer to your question is that C++ is more related to C# than the UnityScript (Javascript like) that Unity possesses. Unity also supports Boo, but I have no idea what it is like as I am purely a Javascript type of person.

    Just starting, with programming experience is always a plus in here. If you are at all like me, I picked up some simple scripts I found in the forums or wiki and started playing. I found that Javascript was alot easier to deal with as you don't have to start off declaring classes and such, they just start working and I can get into the meat of the work without a whole lot of extra coding.

    I am a VBA programmer by heart, not necessarily a C# programmer but I do understand what and how it is working in comparison to Javascript, so I am good with either language.

    From my experience, I sat back and started working on small scripts towards my end goal, all the while looking at questions posed on the forum. "How do I do this" one person asks, and instead of just saying, "Look at the documentation" or "It is on the forum here somewhere" I actually coded the answer, made it work in Unity and gave it back to the requester. Does that teach them anything? NO, but it sure teaches me how Unity works. ;) Not that I am suggesting that you follow the same path...
     
  4. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    Hi Germy,

    Ideally i was hoping i wouldnt have to rewrite in C#;mainly because i wouldnt know where to start. I'm using the free (education) edition of unity.

    the functions i have written in c++ take advantage of OpenCV etc. Is there any way of passing the variables into the unity application? almost like client server type function where my current code runs its functions and then some how sends its resulting variables to the independent unity application.

    As i said im just testing the water with potential solutions before i commit to getting down to the code.

    Thanks
    -K
     
  5. Germy76

    Germy76

    Joined:
    Dec 1, 2010
    Posts:
    31
    Hey,

    Using the DllImport() sounds exactly like what you want. It takes your C++ code and functions and allows you to execute them in C#. Like I said before however I think that you can only do this with Unity Pro. So, poop. As far as doing it other wise I wish I could be of more help. Fortunately I found C# super easy compared to C++ and I imagine you will too.

    I can link you to some tutorials that I love, if that helps get you started in the C# world if you would like, but there are so many out there I doubt you need help finding them. Also if you're using Visual Studios then it builds the code for you and you may not even need a tutorial.

    I wish you luck, I am sorry I could not be of more help.
     
  6. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    Ive had varying experience with everything from HTML, Actionscript, some javascript, PHP, C++ etc. I understand system thinking and can usually pick up the syntax and get my end result (sometimes its easier than other times).

    that said I think I have the ability to complete this aspect of my project using Unity, my concerns lie with the integration of what i already have. In all fairness, this project is mainly a proof of concept, so provided the end result is met, the route taken is largely irrelevent (hence why i am not that bothered about having a single completed application) i guess this isnt the best approach, but in this case i am willing to make an acception :D

    here is kinda what my application is doing:
    I have a 360 Degree camera array which tracks a user
    The cameras track a user and calculate the users position in 3d space (x,y,z)

    THE BIT TO COMPLETE (i am hoping i can do this in unity)
    The xyz co-ords are fed into unity and mapped to a camera. which is locked to viewing a point in space (much like a target cam in 3ds max)

    the camera feed from this camera is then projected onto a plane (positioning is identical to first camera but on separate layer)

    the plane is captured by an ortho camera and this is fed to the screen.

    the idea is similar in nature to this (where the output is skewed but looks correct based on users position): http://vimeo.com/9977433

    Rinse and repeat for new user co-ordinates

    Do you think this is possible?
     
  7. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    I think the best solution would be to run my code independent of unity and some how emulate like a network I/O or something where i can feed my results into unity (so my current code basically controls the unity application)

    I think I would like to learn C# for the sake of it. I am pretty amazed by what unity can do and can see myself using it for projects down the line. For this project, its just too close for me to be attempting a full rewrite :(

    do add your links for C# if its not too much trouble, It would be a nice stepping stone to start a new coding adventure :D

    thanks for the replies.
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I believe that Unity will only work with Managed DLL's (C#) don't quote me on it. Someone like Andeeee would be more qualified. Pulling it over in c# shouldn't be that big of a pain. If the math is in the library for Unity already, then you may end up using Unity's library rather than substituting your own.

    I tried C++ a long time ago with Ogre and Torque, neither of which were particularly successful for me.
     
  9. Germy76

    Germy76

    Joined:
    Dec 1, 2010
    Posts:
    31
    Hey,

    My favorite site is-
    http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=db25cc88fdc47f4afd9562b6abb1332a

    The good thing about UnityScript and C# is there are similar enough that you can watch a tutorial in UnityScript and convert it to C# easily enough.

    So one of my other favorite tutorials is-
    http://unity3d.com/support/resources/tutorials/3d-platform-game

    The real challenge and will really help you learn is convert what they give you into C#, if they don't already I just remember doing it in Java.

    @BigMisterB- Yes, Unity works with Managed DLL's and unmanaged(C++) I just can't remember if its pro only I think it is. I use them a lot :D
     
  10. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    Thanks for all your help with this guys.

    I'm pretty bummed out that i cant do this without the pro version :( and i definitely cant afford $1500. I was hoping there might be some hack work around (like keypress emulation or something similar)

    I guess i am back to square one (OpenGL solution). Dont suppose anyone on here has any experience with OpenGL, had very little luck with the begginers forum on Opengl.org.

    thanks again. will leave this open incase anyone has any inventive solutions **fingers Crossed**
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Perhaps if you laid out your plan in the collaboration forum here, you may garner support for converting your project over. If I remember, in GL you have to make and draw all your objects as you go. All that engine work is done by Unity, all you have to do is tell it where the camera is, and where your model is. (It would not hurt to play with unity for a few days to get a good grasp on what it does before abandoning it)
     
  12. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    You can do it with the free version - you can send data over UDP/TCP or read write from a file or pipe.. Not as performance optimized as direct access though.
     
  13. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    yeah i think i will keep going with trying to get a solution. If i cant figure it, atleast ill rest knowing ive exhausted all avenues beforehand. I would throw it into the collab forum but as its a graded project (its my final) im reluctant to allow for that much assistance incase it is misconstrued as plagiarism. Given that Ive gone way out of my comfort zone and also my course criteria it would be something pretty cool both personally and academically if i can actually achieve what im setting out to do. I WONT GIVE UP!


    This is the sort of solution i was thinking of. Its a real quick and dirty approach, but considering the constraints I have it may be the only solution i have. would UDP/TCP on localhost be able to transmit 3 floats (my X, Y and Z) variables 60 times per second (maximum)??

    priority is on working prototype over performance.
     
  14. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    I'm pretty sure that wouldn't be a problem. You could mock-up a simple test; if you do I'd be interested in your findings.
     
  15. cosmodog

    cosmodog

    Joined:
    Jan 5, 2010
    Posts:
    45
    I did something like this years ago in OpenGL - cool stuff.
    The problem you may have with UDP/TCP is syncing the correct video frame with the correct transform data.
    If you just need primitive graphics (like in the video), I recommend using OpenGL - check out NeHe tutorials:
    http://nehe.gamedev.net/
     
  16. fireapache

    fireapache

    Joined:
    Sep 18, 2010
    Posts:
    37
  17. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Why not just use the Ogre3d or something that works better with C++?

    I could be mistaken but it seems you're got a square peg and you're trying to shove it into a round hole.

    The primary reason to use Unity is the editor, feature set and asset workflows but it sounds like you don't really want/need any of that and are looking for more of a straight graphics renderer to bolt on to your existing C++ application. If that's the case, Unity probably isn't the right tool for the job.
     
  18. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    For raw C++ fighting, I would join Cameron, go with C4, Torque 3D, Leadwerks or OGRE instead which are C++ engines with more or less editor focus (C4 pretty strong one, Torque offers it if you want to use but no must, leadwerks has it as semi must and ogre has none nor even physics as pure rendering until you hook in the rest)
     
  19. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    Thanks guys for all of the responses.

    @ Cameron

    that is how i feel at the moment, there is very little progress happening on this project at the moment and this is mainly due to the fact that i am still searching for a feasible solution.

    Technically Its possible to create the entire thing in OpenGL but as already stated, why reinvent the wheel. I will look into ogre3D today. my main worry is the ability to create asymmetric viewing frustums AKA the ability to create the anamorphic distortion for the viewing.



    thanks again for all your help guys!
     
  20. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You have the source there so you can implement whatever you want, you just have to learn how :)
     
  21. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    That is where the challenge lies. I have only been coding c++ for 4 months. Its a steep learning curve and time is definitely not on my side.

    I'll persevere :)
     
  22. kshaaban

    kshaaban

    Joined:
    Feb 10, 2011
    Posts:
    24
    Hey everybody!

    So it looks like im back!

    Ive managed to find a solution to my above issue.

    Ive managed to port my data into unity through UDP. It seems to be working perfectly fine and i have my XYZ values coming in and controlling a camera beautifully. :D

    Given the months of pondering and hesitation ive decided to really push to get a solution from this. I must admit that things have been somewhat messy but im getting somewhere with it all.

    Again i am here and would really love to get a helping hand from someone to really break down an aspect which i am struggling to comprehend.

    That would be using off Axis projections. Now i am aware of what they are and what they work to do but i am struggling to understand how best to manipulate one to give the perspective distortion i am hoping to achieve. basically this distortion http://vimeo.com/10776715 (about 1 minute in)

    my in scene camera is now moving in correlation with my real world user, mimicking the exact head movements of the user. however I need to be creating the perspective distortion to give the effect the object has volume.

    I would greatly appreciate it if someone was able to help me fully understand the manipulations required to obtain this effect. I have already got the off axis projection code from unity and have applied it to my camera but that is as far as i have got.


    really hope someone can help me. slightly short of time on this one so would be an absolute godsend if anyone could get this into my skull :D

    thanks
    Khaled