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

In summary: The position and velocity updates should still work correctly, but the object might end up moving a little bit more than it should.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.
  • #1
EricMiddleton
3
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
  • #2
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.
 
  • #3
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.
 
  • #4
The accelerations calculation will have to determine that objects at rest on a solid surface end up with zero acceleration.
 
  • #5


It sounds like you have identified the issue correctly. The 'twitching' behavior is likely due to the collisions being checked before the new positions are updated. This can cause the object to get stuck in a loop where it is constantly being pulled through the floor by gravity and then popped back up by the collision detection.

To resolve this issue, you may need to adjust the order in which your simulation operates. One possible solution would be to update the positions first, and then check for collisions. This way, the object's position will be correctly updated before any collisions are detected.

Another solution could be to implement a more sophisticated collision detection algorithm that takes into account the object's current velocity and position. This can help prevent the object from getting stuck in a loop.

Overall, it's important to carefully consider the order in which your simulation operates and make sure that all components are working together correctly. It may also be helpful to test and debug your simulation with different scenarios to ensure that it is functioning accurately.
 

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

1. Why is the object twitching on the ground in my physics simulator?

The object is most likely twitching due to instability in the simulation or a collision with another object. It could also be caused by incorrect physics settings or an error in the code of the simulator.

2. How can I fix the twitching of the object in my physics simulator?

First, try adjusting the physics settings such as the mass, friction, and collision properties of the object. If that doesn't work, check for any errors in the code of the simulator or try re-running the simulation. You may also need to simplify or modify the shape of the object to prevent collisions.

3. Is the twitching of the object in my physics simulator normal?

No, twitching is generally not a normal behavior in a physics simulator. It can be a sign of instability or incorrect settings and should be addressed to ensure accurate simulations.

4. Can the twitching of the object affect the results of my simulation?

Yes, the twitching of the object can affect the accuracy of the simulation results. If the object is not behaving as it should, it can lead to incorrect data and affect the overall outcome of the simulation.

5. Is there a way to prevent the object from twitching in my physics simulator?

There are several ways to prevent object twitching in a physics simulator. These include adjusting physics settings, simplifying the object's shape, and ensuring there are no errors in the code. You may also need to use a more advanced physics engine or adjust the simulation parameters for a more stable outcome.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
29
Views
6K
  • Mechanics
Replies
10
Views
2K
  • Mechanical Engineering
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
Replies
4
Views
4K
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
11K
  • Classical Physics
Replies
15
Views
2K
Back
Top