jaipack17 / Nature2D

A 2D physics engine for Roblox. Create versatile physics simulations and mechanics with GUIs!
https://jaipack17.github.io/Nature2D/
MIT License
141 stars 8 forks source link

Move a rigidbody without freezing it in the air #40

Closed jammees closed 1 year ago

jammees commented 1 year ago

Description

What do you need help with/what is your query? Describe in 30-40 words or more!

Lately I started working again on the camera, but I have this problem that I can't move without making the unanchored rigidbody(s) freeze in the air.

Is there any other option to move a rigidbody without making it freeze in the air? I know :ApplyForce() exists but I need precision

So far I have

What have you tried so far? (Optional)

I have tried using :SetPosition() but it just made the rigidbody freeze. (In RenderStepped)

Lastly

Anything else you would like to share?

https://user-images.githubusercontent.com/86011536/183719608-dabf9319-1322-46e1-9a27-3756618574fe.mp4

jaipack17 commented 1 year ago

I'll take a look a deeper look at this today, I might have a possible fix! Could you possibly attach a demo file?

jammees commented 1 year ago

Sorry that it took me this long to respond. Here's a repo containing the demo:

https://github.com/jammees/Nature2D-Rethink

The camera can be found in manual, in-game it's located in PlayerScripts. I added a method :MoveBy() in rigidbody. It simply sets a value then after :Update it sets the position by that value.

jaipack17 commented 1 year ago

I have a feeling that this problem has to do with the MoveBy() method. Somewhere or the other, it desyncs and ends up moving the rigid body more than it should. Also possible that the MoveBy() method is being called at the wrong time in some cases. I still recommend trying out ApplyForce() by calling it just once with the force and without a time parameter.

Also why not just use SetPosition()?

jammees commented 1 year ago

Hi!

I have tried :ApplyForce before but it's not precise at all, and acts weird (see video below). :MoveBy() is basically :SetPosition, the only difference is that :MoveBy() uses CalculateCenter() function and it adds the value you gave in :MoveBy() to it. Then it calls :SetPosition().

In the video the SetPosition method acted a bit weird for some reason

https://user-images.githubusercontent.com/86011536/184388962-a33325b3-6b22-4c45-8a1b-40078f90e739.mp4

jaipack17 commented 1 year ago

I see, have you tried debugging the value of deltaPos and figured out the cases in which the rigid body acts weird (using MoveBy())

jaipack17 commented 1 year ago

Along with that, also try debugging the positions of the vertices and edges of the rigid body by displaying the points and constraints as guis on screen and the green gui frame and see if they align properly. It might be possible that the frame is misaligned with the point-constraint structure of the rigidbody.

jammees commented 1 year ago

Hi!

I still don't really know how I could move a rigidbody without making it freeze in the air.

:ApplyForce is really close at solving my problem but the delta position I calculated is too big for it since it makes the body fly and I can't keep dividing it and seeing what's the best value.

:SetPosition works perfectly for anchored bodies, the movement is 1:1. However the same thing does not apply to unanchored rigidbodies. It just makes the unanchored body freeze in the air, since I'm constantly calling :Render() that results in calling :SetPosition(). I tried making that the position updates before the physics stuffs or after but none worked.

Is there any way to use :SetPosition() while the object keep's it's forces?

jaipack17 commented 1 year ago

Is there any way to use :SetPosition() while the object keep's it's forces?

You can try looping through the points of the rigid body body:GetVertices() and store the points forces point.forces somewhere. Then call body:SetPosition() and apply the forces you stored earlier to the point.forces again one by one.

jammees commented 1 year ago

Hi!

Now the rigidbody does not freeze! Thank you for helping me with that. However now my problem is that the rigidbody goes through the other one. So basically collisions do not exist anymore:

I have tried editing the engine a bit that after a collision happening it would save it Collisons.LatestCollisionalForces Then in the Camera I would use :SetPosition() with Collisons.LatestCollisionalForces.depth. But it had no effect on the body.

https://user-images.githubusercontent.com/86011536/184555908-6056e94c-28ea-4b3b-b8fb-70cce4f50571.mp4

(I'm very sorry that this issue is still a thing) (I'm getting pretty annoyed how many problems I'm getting with Camera)

jaipack17 commented 1 year ago

Try setting the position before the collision checks take place, maybe that should fix it.

jammees commented 1 year ago

Hi!

Sorry for responding late, but I kinda got burnt out. My progress on my engine is already delayed enough, and it just annoys me that the camera is not working as expected. Since I really want to just release it now and relax.

Anyways back to the camera. What I don't understand is that the position update already happens before any collision checks. (In :Update)

github-actions[bot] commented 1 year ago

Looks like there hasn't been much activity going on here! In order to prevent clutter and purge inactive issues, this issue will be closed if it remains in the state of inactivity. If your issue has not been solved, you can open another issue to address your query! Be sure to format your issue correctly to help us review and process your issue faster!

jammees commented 1 year ago

By some miracle, after months I somehow did it 👍 👍 👍

Here's the code that took me months to figure out:

function Camera.Render(deltaTime: number)
    if Camera.ShouldRender == false or prePosition == Camera.Position then
        return
    end

    for _, objectData: ObjectData in ipairs(Camera.Objects) do
        -- Calculate the movement of the camera since the last update
        local cameraMovement = Camera.Position - prePosition

        -- Calculate the new position of the object by adding the camera movement to its original position
        local newPosition = objectData.OriginalPosition + UDim2.fromOffset(cameraMovement.X, cameraMovement.Y)

        if objectData.IsRigidbody then
            local vertexCache = {}

            for _, vertex in ipairs(objectData.Object:GetVertices()) do
                --vertexCache[vertex.id] = vertex.forces
                --vertexCache[vertex.id] = vertex:Velocity()
                vertexCache[vertex.id] = vertex:Velocity2()
                --vertexCache[vertex.id] = vertex:GetNetForce()
            end

            --objectData.Object:SetPosition(newPosition.X.Offset, newPosition.Y.Offset)
            local newPos = objectData.Object:GetCenter() + cameraMovement
            objectData.Object:SetPosition(newPos.X, newPos.Y)
            --objectData.Object:ApplyForce(cameraMovement.X, cameraMovement.Y)

            for _, vertex in ipairs(objectData.Object:GetVertices()) do
                vertex:ApplyForce(vertexCache[vertex.id])
            end

            objectData.OriginalPosition = newPosition

            continue
        end

        -- Use the ChangePosition method to directly set the new position for the object
        objectData.OriginalPosition = newPosition
        objectData.Object.Position = newPosition
    end

    prePosition = Camera.Position
end

It's still very buggy and choppy, but it's a step in the right direction! Thank you, for helping me out in the beginning!