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.
  2. Dismiss Notice

Need JS help "faking" limited gravity...

Discussion in 'Scripting' started by grenpopnougrenop, Aug 2, 2014.

  1. grenpopnougrenop

    grenpopnougrenop

    Joined:
    Jul 17, 2013
    Posts:
    9
    Hi everybody, first post, and first real poke into Unity... I am hoping to get some advice on simulating some very limited physics (in JavaScript if it makes any difference):

    I have a terrain map where the player can place and stack 1x1x1 blocks that snap into place (aligned with each other) to form "walls" or "buildings." Individual blocks can be destroyed, and the blocks above the created "hole" fall down to fill it... they don't rotate at all, and just move straight down (and up) in the Y axis.

    Currently I am using the built in physics, with some beautiful effects, but I am hitting limitations with PhysX
    (too many pairs errors). In researching the error, I have read posts where people suggest "faking it with maths" but I have no idea where to start. Ultimately, efficiency is key as I am hoping to have 100 times more blocks on camera before I am done...

    I would like to simulate their falling with perhaps some slight bouncing. PhysX is great in that the blocks look and act like granite (the intended feel) and they pop up a little when one below "blows up," then they fall down to the ground and bounce back a little and settle.

    Does anyone have any ideas or inspiration for how to do this efficiently in script? I would love to have massive numbers of blocks.

    Thanks for any ideas!

    -G
     
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    If you asking how to make your own physics engine in unity, thats a large process. I don't know how I would start that. If I were to take on that challenge I'd start with something simple like getting an object to fall. You can make a custom physics script. I'm not a great programmer, but here I go...
    Code (js):
    1.  
    2. var gravityAcceleration : float = 2.0;
    3. var maxGravity
    4.  
    5. private var currentFallSpeed : float;
    6.  
    7. function Update ()
    8. {
    9.      if (useCustomGravity)
    10.      {
    11.           this.transform.position.y -= gravityStrength * gravityAcceleration * Time.deltaTime
    12.           currentFallSpeed = distance/time //It starts to get complicated for my little brain very quickly
    13.           if (currentFallSpeed >= maxFallSpeed)
    14.           {
    15.                gravityAcceleration = 1;
    16.           }
    17.      }
    18. }
     
  3. grenpopnougrenop

    grenpopnougrenop

    Joined:
    Jul 17, 2013
    Posts:
    9
    Thanks for the reply. I have thought to just push everything down, but that seems like it would be inefficient if I am doing that to everything all the time... I will try it and see.

    I guess where my question is going then is to figure out how to only move blocks that need to be moved. Like make up some way for them to talk to each other so that when one dies, the ones around it know to react. Use the fake gravity you suggested, move them, then forget about everything until another block dies...

    Are there ways to have the block that is exploding "look straight up and see if anyone is above you, then tell them to react"?

    I would hate to have to keep a matrix of every block in the scene as that seems inefficient too.
     
  4. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I guess you could Raycast
     
    grenpopnougrenop likes this.
  5. grenpopnougrenop

    grenpopnougrenop

    Joined:
    Jul 17, 2013
    Posts:
    9
    I will try raycasting too. It sounds like a good approach. Thank you for the suggestion.

    Is it efficient? It seems like the best option, but I may have dozens of these going off in the same frame, and they will have to cast out from all 6 faces...

    Has anyone pushed the bounds of Raycast? Anything to avoid? I am hoping it is trivially cheap.
     
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    You could use a layer mask which will limit the raycasts to only hit stuff in x layer
     
    grenpopnougrenop likes this.