Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A deterministic physics simulation using Verlet algorithm in Godot 4.0

License

NotificationsYou must be signed in to change notification settings

svdragster/Godot-Verlet-Solver

Repository files navigation

A Verlet Algorithm implemented in Godot 4 with some examples. My long term goal is to create a deterministic physics engine where an equal input always results in the same output.

Example

When running the simulation with the exact same input, the circles will always end up in the same positions with the same rotations:

GodotVerletProject.mp4
GodotVerletProject_3d.webm

Examples can be found in theexample directory. (3D solver is currently developed in3d branch)

How it works

All children are updated in_physics_process with multiple substeps._physics_process is run with a fixed timestep (default is 60 times per second).

func_physics_process(delta :float)->void:varsub_delta :float=delta/substepsforiinrange(substeps):check_collisions(sub_delta)update_constraints(sub_delta)update_objects(delta)

This is the main implementation of the algorithm. The next position is always based on the last position.

funcupdate(delta :float):varvelocity :Vector2= (position-last_position)*frictionvarnew_position :Vector2=position+velocity+ (acceleration*40)* (delta*delta)  [...]

Using Godots Shape objects, I implemented a basic collision handling to update velocity and angular velocity.

varcontacts:=shape_a.collide_and_get_contacts(object_a.transform,shape_b,object_b.transform)varcontact_amount=contacts.size()/2ifcontact_amount>=1:foriinrange(0,contacts.size(),2):# Solve collision

Limitations

  1. It's currently not very realistic. Using more realistic equations (friction, intertia, mass) to solve collisions should be pretty easy to implement.
  2. Currently, Godot 64-bit double-precision floats are used for calculations (equal to double in C++). For data structures such as Vector2 and Vector3, Godot uses 32-bit floating-point numbers (Source:https://docs.godotengine.org/de/stable/classes/class_float.html). This could result in different behaviour on different machines. My long term goal is to use fixed point numbers so the calculations will behave the same on all systems.

Goals

These are somwhat in order

  • WIP: support more shapes for collisions (2d currently supports spheres and rectangles, see directoryverlet2d/shapes)
  • WIP: 3d implementation (see branch3dhttps://github.com/svdragster/GodotVerletSolver/tree/3d/verlet3d)
  • add fixed point numbers
  • use chunk system for collisions
  • multithreading chunks
  • Implement in GDExtension (C++) for better performance

About

A deterministic physics simulation using Verlet algorithm in Godot 4.0

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp