Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D Physics with custom world matrix?

Discussion in 'Physics' started by C-Gabriel, Aug 30, 2019.

  1. C-Gabriel

    C-Gabriel

    Joined:
    Jan 27, 2016
    Posts:
    119
    Hi,

    Is it possible to manually set (preferably at run-time) a custom world matrix for the 2D physics? I want to have some rigid bodies always on a plane, but the plane may rotate.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Physics isn't like a rendering API which is what I presume you mean by "world matrix" so there's not a base transformation for them all you can apply.

    That said, I could provide some options if you could provide more info on what you want which sounds like you want a group of bodies to rotate together around the Z axis? If so then this would depend on the body types i.e. dynamic, kinematic or static and what they might be colliding with, if anything. Several options using joints and/or separate physics scenes but again, depends on what you want.
     
  3. C-Gabriel

    C-Gabriel

    Joined:
    Jan 27, 2016
    Posts:
    119
    Hi @MelvMay, the idea is to have some rigid bodies slide on a planar surface (defined by parent.forward and parent.position), but the surface will have a variable rotation controlled by some user input. For example, a sphere (circle in 2D) will always have the gravity vector equal with parent.down, instead of Vector3.down

    It could be done with 3D physics, but I'm thinking that I could save some performance doing it in 2D, since I want the rigid bodies to always be on the surface. Plus I'd avoid objects going through the plane at corners.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Well obviously 2D physics is always on the XY plane so there's nothing to do there but I'm still not sure what you want to happen when this "plane" rotates and exactly what you mean by "slide"? Do you want all these specific rigidbodies to rotate with it around a fixed point? It's this part that's not clear.
     
  5. C-Gabriel

    C-Gabriel

    Joined:
    Jan 27, 2016
    Posts:
    119
    The 2D physics is always planar, of course, but a plane can be defined by any vector + position, right? I want to have sort of a pool table on a polygon of any 3d object.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    2D physics works on the XY plane only, not an arbitrary plane so no, you cannot have it transformed into a 3D plane. I had assumed because of your first post you were talking about rotating around the Z axis a group of Rigidbody2D.

    Of course, you could perform the simulation in 2D and grab the transform pose, applying an arbitrary transform into your plane-space and assign that to your render objects. Unless you're talking about thousands of objects this shouldn't be a problem.
     
    Last edited: Sep 1, 2019
    C-Gabriel likes this.
  7. C-Gabriel

    C-Gabriel

    Joined:
    Jan 27, 2016
    Posts:
    119
    You're right. I could simulate it in the 2D plane and transform only the meshes