Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Box2D C# Port + Unity

Discussion in 'Scripting' started by Crash-512, Sep 13, 2011.

  1. Crash-512

    Crash-512

    Joined:
    Sep 13, 2011
    Posts:
    6
    Hi guys!

    There is Box2D C# port:
    http://code.google.com/p/box2dx/

    I want to use it in Unity, but one problem:
    this version uses unsafe-code and pointers, and Unity denies compile unsafe-code.
    Unsafe code is present in only 3 classes on this port: Distance.cs, TimeOfImpact.cs, ContactSolver.cs.

    For example:

    Code (csharp):
    1.  
    2. internal unsafe void ReadCache(SimplexCache* cache, Shape shapeA, XForm transformA, Shape shapeB, XForm transformB)
    3. {
    4.     Box2DXDebug.Assert(0 <= cache->Count  cache->Count <= 3);
    5.  
    6.     _count = cache->Count;
    7.     SimplexVertex** vertices = stackalloc SimplexVertex*[3];
    8.     ...
    9.     ...
    10. }
    11.  
    I don't have much C#-experience to change this classes and compile it for Unity3D.
    Any ideas? I know, many people want to use Box2D in their projects.

    P.S. Sorry for my English:)

    Cheers,
    Max.
     
  2. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    just take out the unsafe and the pointers and it should work. and Im assuming cache is an array so take out the -> and just put a period
     
  3. Crash-512

    Crash-512

    Joined:
    Sep 13, 2011
    Posts:
    6
    I tried, but tt's difficult for me. To much difficult code...
     
  4. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    i think theres an option somewhere in unity that will let you use unsafe code. I dont remember where though. And it wont work on web build
     
  5. joel

    joel

    Joined:
    Jun 12, 2010
    Posts:
    117
  6. Crash-512

    Crash-512

    Joined:
    Sep 13, 2011
    Posts:
    6
  7. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Interested in hearing unsafe was once posible in 2.2. Any reason its not in 3? and is there a way to get /unsafe in 3.4?

    Crash-512: if its only a few spots where unsafe code is used, try no-pasting them and i'll see what i can help you with.
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    potentially unsafe code got removed to get rid of the pro limitation cheaters beside the fact that unitys native code support is limited to the InteropServices related stuff in pro and likely has some modifications on the compiler level in unity (unity is no stock mono in u3 far less than already in u2)
     
  9. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    So its not likely or imposible to use unsafe code?
     
  10. PAX

    PAX

    Joined:
    Oct 12, 2010
    Posts:
    54
    I created a simple integration of box2d to Unity few month ago, but development stopped.

    $box2d.png

    Someone interested in this development?
     
    Last edited: Sep 21, 2011
  11. Crash-512

    Crash-512

    Joined:
    Sep 13, 2011
    Posts:
    6
    Мне интересно! Меня даже интересует только код, без интеграции в редактор.
     
  12. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127


    hello,
    can you share your work out?
    Its cool, and I need to use box2D now.
    thank you :)
     
    Last edited: Sep 29, 2011
  13. tiagoamorim

    tiagoamorim

    Joined:
    Oct 17, 2011
    Posts:
    1
    I tried many different b2d ports and the easiest to get it to work was box2dx. I just placed their dll on my assets folder and added the appropriate "using" keyword to whatever I needed from it. Only issue is that this port is based on an older box2d version, 2.01, I believe. But so far everything seems to be working fine for me.
     
  14. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
    I dont understand,
    I put the dll called Box2DX.dll in the assets folder
    and make a c# file with "using Box2DX;" line.
    but I still cant use any box2d class ,
    why?
     
  15. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
  16. Crash-512

    Crash-512

    Joined:
    Sep 13, 2011
    Posts:
    6
    rex170, use FarseerPhysics: farseerphysics.codeplex.com

    Box2D don't works in Unity3D.
     
  17. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
    thanks, 512.
    so , how do I get started to use the basic "World" object?
     
    Last edited: Oct 25, 2011
  18. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
    seems FarseerPhysics uses namespaces ,
    and Unity doesnt support it , right?
     
  19. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
    :p
    I just found the way to use box2dx in unity3d.
    the problem for me now ,is to render the world in unity.
    hope PAX can help us lol
     
  20. Crash-512

    Crash-512

    Joined:
    Sep 13, 2011
    Posts:
    6
    rex170, just use refactoring to change name of following classes:
    Vector2, Vector3, Joint.

    Also, PAX uses farseerPhysics too:)
     
  21. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
    so , can it be used easily like builtin physics engine in unity3d?
    like just add a body component to a object then works?
    can you teach me how to get started?
     
  22. PAX

    PAX

    Joined:
    Oct 12, 2010
    Posts:
    54
    Here is the source of the project, enjoy!
     

    Attached Files:

  23. Hao-Cher-Hong

    Hao-Cher-Hong

    Joined:
    Dec 15, 2010
    Posts:
    127
    thank you very much :)
     
  24. PAX

    PAX

    Joined:
    Oct 12, 2010
    Posts:
    54
    Ported version is Change Set 90041 of Farseer Physics sources
     
  25. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    You are far too kind!
    Thank you very much sir!
     
  26. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    Hi, i've just come across this, thanks for posting the sources - very cool.

    I've noticed there is no interpolation option on dynamic bodies as there is with PhysX. I'll look to add that myself but I was wondering if anyone has done anything similar, or if there is another way to help sync physics with framerate?

    Thanks.
     
  27. joeyxiong

    joeyxiong

    Joined:
    Dec 13, 2011
    Posts:
    10
    thank you for sharing the source. But how can I query collisions? such as raycast, OnCollisionEnter, OnTriggerEnter...
     
  28. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    As far as I could tell the collision callbacks aren't exposed in the port so you'd have to implement that yourself.

    Be aware that Farseer/Box2D uses a different structure for the callbacks so it might be fair bit of work to get it to match Unity's way of doing things. Only one way to find out though :)
     
  29. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,617
    Someone should make a Box2D Asset.
     
  30. Ava

    Ava

    Joined:
    Oct 19, 2009
    Posts:
    2
    Hello! Someone tried to use the class MSTerrain?
     
unityunity