Coder Social home page Coder Social logo

Comments (13)

jammees avatar jammees commented on May 26, 2024 1

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.

from nature2d.

jaipack17 avatar jaipack17 commented on May 26, 2024 1

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.

from nature2d.

jammees avatar jammees commented on May 26, 2024 1

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.

2022-08-14.23-36-43.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)

from nature2d.

jaipack17 avatar jaipack17 commented on May 26, 2024

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

from nature2d.

jaipack17 avatar jaipack17 commented on May 26, 2024

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()?

from nature2d.

jammees avatar jammees commented on May 26, 2024

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

2022-08-12.17-29-55.mp4

from nature2d.

jaipack17 avatar jaipack17 commented on May 26, 2024

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

from nature2d.

jaipack17 avatar jaipack17 commented on May 26, 2024

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.

from nature2d.

jammees avatar jammees commented on May 26, 2024

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?

from nature2d.

jaipack17 avatar jaipack17 commented on May 26, 2024

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

from nature2d.

jammees avatar jammees commented on May 26, 2024

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)

from nature2d.

github-actions avatar github-actions commented on May 26, 2024

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!

from nature2d.

jammees avatar jammees commented on May 26, 2024

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!

from nature2d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.