Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

Is it possible to change the way physics is computed in GODOT?

I've got an idea for a simple puzzle game where the player controls the basic physics laws and formulas. However, to achieve this, I'd need to somehow override GODOT's physics. I can figure out how to make player interface for this myself, but I first need to see if I can actually change the physics. For example: is it possible to change

acceleration = Force/mass

to

acceleration = Force * mass

?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two approaches. You can build up from KinematicBody or customize the RigidBody.

Using the KinematicBody you can write the movement however you want. Just write it in _physics_process. You can handle the collisions by using move_and_collide which returns a KinematicCollision object where you get the info of the collisions, allowing you to write your own collision resolution.

Using RigidBody, I suggest to set custom_integrator to true and override _integrate_forces which is called during the physics process with a PhysicsDirectBodyState allowing you to read and write the state of the physics object.

Regardless of the approach, for the modification you want, you will have to keep track of the acceleration yourself, and set the velocities. The RigidBody can keep track of forces for you, or you can do it on your own. You will be writing your physics integration step regardless.

Know that the different physics nodes (including joins/constraints) are all using the PhysicsServer api behind the scenes. Which you could call directly if you so desire (this could be to optimize your code, or to do something the nodes do not provide out of the box).


It is, in theory, possible to provide your own physics engine for Godot (as it currently ships with two for you to pick from). I haven't seen documentation on how to do that. I only know for sure that you will be building from source (which isn't that hard).

I also want to mention that Godot physics is getting a rewrite for Godot 4.0 (it probably won't ship with two anymore). And a new API to provide the back end for the PhysicsServer is expected for Godot 4.1 (so plugging your own physics engine won't be a dark art anymore, we can expect third party physics engines to be a thing at some point).


Addendum: There is another approach, actually. Use Godex, and write your physics using its Entity-Component-System architecture.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...