FreshRSS

Normální zobrazení

Jsou dostupné nové články, klikněte pro obnovení stránky.
PředevčíremHlavní kanál

How to get consistent collision impulse values for colliding rigid bodies in Godot?

I'm writing a game where you can pick and throw objects and depending on the force of the impact they break.

Using the get_contact_impulse method to calculate the breaking point gives me very consistent results, but only for a single rigid body. When two rigid bodies collide, one of them reports the correct impulse, the other reports really low values, close to 0.

As an example, this is the same collision being reported by both objects:

SimpleCube_A2 collide with SimpleCube_A force 18.338762
SimpleCube_A collide with SimpleCube_A2 force 0.0180135

I believe this happens because once the second object runs its IntegratePhysics, the impact has already been solved by the first object (but this is just a guess).

I could just call a method on the second object, but since both objects will be reacting to the impact, I would have to add some bookkeeping to make sure the calls don't get recursive, nothing difficult but could be more error prone.

So I'm wondering if there's a solution where both objects can detect the "same" impulse, but without talking to each other. I don't mind having to calculate the impact myself or something like that, as long as it's reliable enough.

  • ✇Recent Questions - Game Development Stack Exchange
  • Rigidbody Simulated vs Static vs noneCheckerT
    I am confused about the usage of static / not simulated rigidbodies. I get that it is better to disable simulated to temporarily stop a rigidbody rather than deleting and recreating it, especially because all references wont get destroyed, but why are there two options to stop it? What is the difference between a static rigidbody and a not simulated rigidbody? Also I have heard that peoplre create a lot of colliders with static/not simulated colliders. Why bother to create rigidbodies at all if
     

Rigidbody Simulated vs Static vs none

I am confused about the usage of static / not simulated rigidbodies.

I get that it is better to disable simulated to temporarily stop a rigidbody rather than deleting and recreating it, especially because all references wont get destroyed, but why are there two options to stop it? What is the difference between a static rigidbody and a not simulated rigidbody?

Also I have heard that peoplre create a lot of colliders with static/not simulated colliders. Why bother to create rigidbodies at all if the gameObject is never meant to move? Doesn't this just waste processing power to a thing that is completely unnecessary?

Why do I have to freeze my player's x and z rotation to make my player movement work and why does my camera stutter when colliding with things?

So I have an empty object called player with the PlayerMovement script and a rigidbody. I have the main camera and player visual as childs of it. The movement works fine as long as I freeze the rigidbody's x and z rotation, but why is that? Here is my player code (minus a couple of things that would just make it harder for you to help me):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
    {
    [SerializeField] private float speed = 5f;
 

    private float xLook;
    private float yLook;

    [SerializeField] private float _mouseSensitivity;
    [SerializeField] private float _upAndDownMax;

    [SerializeField] private Rigidbody rb;
    private Camera _camera;

    private Vector3 movement;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        _camera = Camera.main;
    }

    void Update()
    {
        HandleMoveInput();
        HandleLookInput();
    }
     private void HandleMoveInput()
     {
        float hor = Input.GetAxisRaw("Horizontal");
        float ver = Input.GetAxisRaw("Vertical");

        Vector3 moveInput = new Vector3(hor, 0, ver);
        moveInput = transform.forward * moveInput.z + transform.right * moveInput.x;
        moveInput.Normalize();
        movement = moveInput * speed;
     }
    private void HandleLookInput()
    {
        xLook += Input.GetAxis("Mouse X") * _mouseSensitivity * Time.deltaTime;
        //  transform.rotation = Quaternion.Euler(0, xLook, 0);
        rb.MoveRotation(Quaternion.Euler(0, xLook, 0));

        yLook -= Input.GetAxis("Mouse Y") * _mouseSensitivity * Time.deltaTime;
        yLook = Mathf.Clamp(yLook, -_upAndDownMax, _upAndDownMax);
        _camera.transform.localRotation = Quaternion.Euler(yLook, 0, 0);
    }

    void FixedUpdate()
    {
        MovePlayer();
       // LookPlayer();
    }
    private void MovePlayer()
    {
        Vector3 currentVelocity = rb.velocity;
        Vector3 targetVelocity = new Vector3(movement.x, currentVelocity.y, movement.z);

        Vector3 force = targetVelocity - currentVelocity;
        force.y = 0; 

        rb.AddForce(force, ForceMode.VelocityChange);
    }
}

So I am guessing the problem is inside HandleMoveInput but I am not sure. I am just wondering why I have to freeze the rotation of z and x to make it work.

The second problem is that the camera sometimes start to stutter when colliding with things. I tested and I believe its because of the freeze problem and that its a child of the player. Because it does not happen when its not a child. Is it a bad idea to make the camera I child of the player?

If it's of any help: Editor screenshot

  • ✇Recent Questions - Game Development Stack Exchange
  • Why is my player not colliding with animated obstacles?Ivan
    I can't figure this out. My obstacles have a basic box collider on them and my player has a rigidbody based controller and collsion detection is set to Continous and when obstacle hits him it just passes trough the player. Belowe is my player rigidbody and collider settings Update: Ok so I found out the following things: Player do seem to react a little when I add a rigidbody to the obstacle, set it to kinematic with continuous collision detection, and I increase player size (this is the most im
     

Why is my player not colliding with animated obstacles?

I can't figure this out. My obstacles have a basic box collider on them and my player has a rigidbody based controller and collsion detection is set to Continous and when obstacle hits him it just passes trough the player. Belowe is my player rigidbody and collider settings

Update:

Ok so I found out the following things: Player do seem to react a little when I add a rigidbody to the obstacle, set it to kinematic with continuous collision detection, and I increase player size (this is the most important change). Still it only moves slightly and obstacle passes trough the player.

enter image description here

❌
❌