- Home /
Object passing through at high speeds
Hi,
I am trying to make 3d smooth tetris. However, objects are passing through others. I created a small project to simulate this.
Project can be downloaded from: https://dl.dropboxusercontent.com/u/2118263/object-passing-through.zip
Here are the notes:
There is a plane, a static cube and a moving cube.
Static cube has position = (4.5,0.5,0), rigidbody, box collider, freeze position, freeeze rotation, no gravity and "Finish" tag.
Moving cube has position = (0, 0.5, 0), rigidbody, box collider, continous dynamic collision detection and Mover Script(below).
Moving cube was moving towards Static Cube, and onTrigger enter, i set the speed = 0 and log the position. The output is "Entered position @(3.6, 0.5, 0)" however I expect it to be exactly "Entered position @3.5, 0.5, 0)". Can you explain why it cannot enter the trigger exactly @3.5.
Because of this problem, I also started to think about not using collisions. Any suggestions to make a 3d smooth tetris?
Thanks.
public class Mover : MonoBehaviour {
int speed = 5;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
rigidbody.MovePosition (rigidbody.position + Vector3.right * speed * Time.deltaTime);
}
void OnCollisionEnter(Collision other){
if (other.gameObject.tag == "Finish") {
Debug.Log ("Entered position @" + rigidbody.position);
speed = 0;
}
}
}
A simple solution to this would be to make the trigger(box collider) slightly bigger by 0.1. Then you can expect it to trigger at 3.5. OR Try increasing iteration solver count in the physics options. This will impact performance but I am not sure to what degree it will affect.
It works on low speed. Updating timestep will do the job, but I wanted to learn how unity works. I didn't want to make calculations to avoid it. Unfortunately as fas as I understood, I should.
Answer by Jamora · Jan 15, 2014 at 03:57 PM
Physics in games aren't contiguous like in real life (as far as we know), but discrete. This means that all physics computation is done in intervals, and these computations include collision detection. In Unity the default interval is 0.02 seconds, i.e. 50 times each second.
Now, let's imagine we have a euclidean coordinate system (x,y) and there is an object at 0,0 with a speed 1 unit/second along the x-axis. Our computation interval is two seconds, so this means we will only notice the object at points (2,0), (4,0), etc...
In Unity, the interval of physics computation can be changed in Edit -> Project Settings -> Time. It is called "Fixed Timestep". Setting that value to 0.1 or smaller will decrease the interval of computation (= CPU usage) but will allow you to notice smaller movements in your objects and possibly allow you to stop your tetris pieces in time.
Instead of using physics, I would build a tetris on a 2D grid. If you look at the tetrises of yesteryear (also lots of the modern ones), you will notice they are very blocky, which means that they too are most likely built on a grid.
Is there a collision detection default interval? Or do you mean Time$$anonymous$$anager.fixedTimestep? I wanted to be sure, because the default value of fixedTimestep is 0.02.
Fixed timestep is the interval at which all physics computation is done. You're correct, the default timestep is 0.02; I missed a zero.. guess it's time to get glasses =/.
The point is, that halving that value will double the physics computation and will allow you to detect smaller penetrations. I still recommend a 2D grid to allow for precision positioning of the blocks. Using Unity's physics (I think it's PhysX)for anything precise seems like a bad idea to me.
There is no Time$$anonymous$$anager.fixedTimestep in the Unity API, but there is Time.fixedTimestep. Time.fixedTimestep is the same value as the one in Edit -> Project Settings -> Time -> Fixed Timestep.