Object is 'twitching' on the ground in my physics simulator

Click For Summary

Discussion Overview

The discussion revolves around a physics simulation issue where an object appears to 'twitch' on the ground due to conflicting forces from gravity and collision detection. Participants explore the mechanics of the simulation, including the order of operations and the handling of collisions, with a focus on debugging the code used in the simulation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the sequence of operations in their physics engine, suggesting that collisions may be checked before positions are updated, which could contribute to the twitching effect.
  • Another participant requests more information about the code to better diagnose the issue, speculating that the velocity vector may not be updated correctly and that the orientation of the floor could affect collision responses.
  • A third participant shares specific code snippets from their prototype in Garry's Mod, detailing how they calculate acceleration, update position and velocity, and handle collisions, noting that moving collision detection later in the code helped somewhat but did not fully resolve the twitching.
  • One participant suggests that the calculation of acceleration should ensure that objects at rest on a solid surface have zero acceleration, implying that this could be a factor in the twitching behavior.

Areas of Agreement / Disagreement

Participants express various hypotheses about the causes of the twitching, but there is no consensus on the exact solution or the underlying mechanics at play. Multiple competing views and suggestions remain unresolved.

Contextual Notes

There are limitations in the provided information, such as the lack of details on the collision detection algorithm and the assumptions regarding the object's properties and the environment. The discussion also highlights potential dependencies on the definitions of forces and the simulation's time steps.

EricMiddleton
Messages
3
Reaction score
0
I am working on a simple physics engine right now and when the object eventually comes to a rest, it starts 'twitching' (moving up/down slightly very quickly) because gravity pulls it through the floor, and then the collision detection pops it back up. Here is the order in which my simulation operates:

Calculate Acceleration (Gravity)
Check prior collisions
Update Position
Update Velocity

The only thing I can think of is that the collisions are being tested before the new positions are updated. Thanks for any help you can provide.
 
Technology news on Phys.org
EricMiddleton said:
I am working on a simple physics engine right now and when the object eventually comes to a rest, it starts 'twitching' (moving up/down slightly very quickly) because gravity pulls it through the floor, and then the collision detection pops it back up. Here is the order in which my simulation operates:

Calculate Acceleration (Gravity)
Check prior collisions
Update Position
Update Velocity

The only thing I can think of is that the collisions are being tested before the new positions are updated. Thanks for any help you can provide.

Can you post a little more information about the actual code? I know its not feasible to dump every routine (even if you did want to), but it's hard to really know specifically is wrong.

I'm wondering if your velocity vector gets the right update. If the collision code is correct, then if gravity is ok and the floor of your map is on the x-y axis (I'm assuming z points up in this case), then the collision should always move the object to the same point (assuming the object is actually or is treated like a box).

But if the floor of your map is at an angle, what may happen is that due to the orientation of your vector and possibly your routine, you might be re-projecting your object at a different place each time and hence you see the weird movement.

Again this is all speculation, and I can't really give a solid comment without getting more information.
 
I'm currently writing the prototype in a game called Garry's Mod using an an extension called wiremod, for ease of debugging. The language is called Expression2, and its very easy to understand.

Code:
T = 1/33 #This code is updated every tick (33 times per second)
Acceleration = vec(0, 0, -gravity())

Position += Velocity*T + Acceleration/2*T^2

Velocity += Acceleration*T

holoPos(1, Position) #update the position (necessary to get proper bounding box translations)

for(Loop = 1, Count) #iterate through each object
{
    E = holoEntity(Loop)
    Box = E:toWorld(E:boxSize()) #Translate the object's bounding box
    if(Box:z() < 0) #if it's below the ground
    {
        Height = Box:z()
        Ang = 90 - Velocity:toAngle():pitch() #calculate angle
        Dist = -Box:z() / cos(Ang) #calculate how far it's gone
        Time = abs(Dist/Velocity:z()) #derive time from the distance
        Position -= Velocity * Time #roll back the position
        T += Time #speed time back up
        Velocity -= Velocity * Time
        Velocity *= vec(1, 1, -0.5) #inelastic collision
        HitLoop = Loop #for debugging
        Velocity -= vec(min(abs(Velocity:x()), Mu*Velocity:z()*Mass)*sign(Velocity:x()), min(abs(Velocity:y()), Mu*Velocity:z()*Mass)*sign(Velocity:y()), 0) * T #friction
    }
}
holoPos(1, Position) #update the position again
I also tried moving the collision detection down near the end of the code (as seen above) which seems to help a little, but not eliminate the issue entirely.
 
The accelerations calculation will have to determine that objects at rest on a solid surface end up with zero acceleration.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
10
Views
5K
  • · Replies 29 ·
Replies
29
Views
8K
Replies
17
Views
3K
  • · Replies 61 ·
3
Replies
61
Views
5K
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 9 ·
Replies
9
Views
2K