Commit graph

331 commits

Author SHA1 Message Date
PouleyKetchoupp 62e577267f Expose body_test_motion in 3D physics server
Results are exposed through PhysicsTestMotionResult3D, the same way it's
done for 2D.

Also cleaned a few things in the 2D version.
2021-07-02 18:03:44 -07:00
Rémi Verschelde 24c6ee9f56
Merge pull request #49844 from nekomatata/physics-disabled-shapes
Fix and clean disabled shapes handling in godot physics servers
2021-06-30 10:08:52 +02:00
PouleyKetchoupp 9758a75221 Fix move_and_collide causing sliding on slopes
Make sure the direction of the motion is preserved, unless the depth is
higher than the margin, which means the body needs depenetration in any
direction.

Also changed move_and_slide to avoid sliding on the first motion, in
order to avoid issues with unstable position on ground when jumping.

Co-authored-by: fabriceci <fabricecipolla@gmail.com>
2021-06-28 17:17:52 -07:00
PouleyKetchoupp a65cdca894 Fix and clean disabled shapes handling in godot physics servers
In 3D, disabled shapes are now not added to the broadphase anymore.
Since they are removed right away when disabled, no need to check for
disabled shapes for any query that comes from the broadphase.
Also Fixes raycast queries returning disabled shapes.

In 2D, disabled shapes where already not added to the broadphase.
Remove the same unnecessary checks as in 3D.

Overall harmonized API for disabled shapes in the physics servers and
removed duplicate method.
2021-06-22 16:51:47 -07:00
reduz c66b2651a6 Refactor CommandQueueMT
* RingBuffer had no reason to be in this context
* A single buffer is used that can grow as much as the game needs.

This should make thread loading entirely reliable.
2021-06-09 13:10:49 -03:00
PouleyKetchoupp b2bd9f4e51 Safe margin cleanup
Safe margin property on CharacterBody only, used as argument in
move_and_collide.

Removed kinematic_safe_margin in 3D physics server, not really useful
and now harmonized with 2D.
2021-06-04 11:40:36 -07:00
PouleyKetchoupp ee4b756a51 More explanatory names for RigidBody modes
MODE_DYNAMIC instead of MODE_RIGID
MODE_DYNAMIC_LOCKED instead of MODE_CHARACTER

No more special case for sleeping behavior for MODE_DYNAMIC_LOCKED
(MODE_CHARACTER was forcing the body not to sleep, which is redundant
with can_sleep and wasn't done in Bullet).
2021-06-04 11:40:36 -07:00
Aaron Franke 94bc0bd919
Rename Vector2 clamped to limit_length and add limit_length to Vector3 2021-06-03 12:04:57 -04:00
Rhathe 88b1802132 Enable setting of collision iterations in PhysicsServer2D
This allows fine-tuning of collision iterations for more
accurate collision physics with a performance cost.
2021-05-19 10:54:10 -04:00
PouleyKetchoupp 3877ed73d0 Dynamic BVH broadphase in 2D & 3D Godot Physics
Port lawnjelly's dynamic BVH implementation from 3.x to be used in
both 2D and 3D broadphases.

Removed alternative broadphase implementations which are not meant to be
used anymore since they are much slower.

Includes changes in Rect2, Vector2, Vector3 that help with the template
implementation of the dynamic BVH by uniformizing the interface between
2D and 3D math.

Co-authored-by: lawnjelly <lawnjelly@gmail.com>
2021-05-10 16:28:55 -07:00
PouleyKetchoupp 448c41a3e4 Godot Physics collisions and solver processed on threads
Use ThreadWorkPool to process physics step tasks in multiple threads. Collisions are all processed in parallel and solving impulses is
processed in parallel for rigid body islands.

Additional changes:
- Proper islands for soft bodies linked to active bodies
- All moving areas are on separate islands (can be parallelized)
- Fix inconsistencies with body islands (Kinematic bodies could link
bodies together or not depending on the processing order)
- Completely prevent static bodies to be active (it could cause islands
to be wrongly created and cause dangerous multi-threading operations as
well as inconsistencies in created islands)
- Apply impulses only on dynamic bodies to avoid unsafe multi-threaded
operations (static bodies can be on multiple islands)
- Removed inverted iterations when populating body islands, it's now
faster in regular order (maybe after fixing inconsistencies)
2021-04-26 18:26:00 -07:00
rafallus cfa06f0f76 Unexpose _direct_state_changed in PhysicsBody
Removed _direct_state_changed bindings
Affects 2D and 3D nodes
Callbacks now use Callable
Tests were changed accordingly
2021-04-22 23:20:58 -05:00
Rémi Verschelde c022582c1e
Merge pull request #47942 from nekomatata/joint-check-body-types
Fix errors related to joints setup with two non-dynamic bodies
2021-04-16 08:51:19 +02:00
PouleyKetchoupp 60ae264db1 Fix errors related to joints setup with two non-dynamic bodies 2021-04-15 17:37:45 -07:00
Henry Conklin 9b65b00296 Optimize BroadPhase2dHashGrid to not pair elements on different layers
Clean up logic in _check_motion

Closes 45824
2021-04-15 17:20:07 -04:00
PouleyKetchoupp b65d6b56fb Godot Physics solver optimization
Several optimizations in the way solver islands are processed in both
2D and 3D physics:
- Use LocalVector instead of linked list to avoid cache misses (with
persistent storage based on worst case scenario)
- Remove pairs when setup fails (no valid contact) to avoid unnecessary
solving of non-colliding rigid bodies just to return immediately
2021-04-12 18:59:49 -07:00
Rémi Verschelde 68b28c6cd4
Merge pull request #47678 from madmiraal/fix-47436.1
Check for empty ConcavePolygonShape2D before checking for intersection
2021-04-06 22:35:49 +02:00
Rémi Verschelde 7fa84d8bd3
Merge pull request #47675 from madmiraal/fix-47436
Check ConvexPolygonShape2D point count before calcuating moment of inertia
2021-04-06 22:35:01 +02:00
Marcel Admiraal 2abfc0518f Check for empty ConcavePolygonShape2D before checking for intersection 2021-04-06 20:05:10 +01:00
Marcel Admiraal 25593a481a Check ConvexPolygonShape2D point count before calcuating moment of inertia 2021-04-06 19:15:09 +01:00
Marcel Admiraal 0d4d8925b7 Ensure ConvexPolygonShape support count variable is initialised 2021-04-06 16:18:49 +01:00
Rémi Verschelde 2bd89ac249
Merge pull request #44695 from madmiraal/fix-44632
Remove all elements from monitored_bodies and monitored_areas when processed
2021-03-26 16:34:02 +01:00
PouleyKetchoupp c5d2404a13 Fix GodotPhysics solver with kinematic body set to report contacts
In 3D, collision is disabled between kinematic/static bodies when
contacts are generated only to report them.

In 2D, this case was already fixed but the code is cleaned to make it
easier to follow.
2021-03-11 18:06:00 -07:00
Eryk Dwornicki fb9d6fcc6d Refactor removal of constraints from bodies in 2D joints 2021-02-27 20:52:01 +01:00
Rémi Verschelde a930472319
Merge pull request #45863 from nekomatata/physics-queries-disabled-shapes
Fix physics queries not filtering out disabled collision shapes
2021-02-24 13:05:28 +01:00
Rémi Verschelde 310496a89f
Merge pull request #45617 from RandomShaper/modernize_atomics
Modernize atomics (and fix `volatile`)
2021-02-18 19:40:31 +01:00
Pedro J. Estébanez 8e128726f0 Modernize atomics
- Based on C++11's `atomic`
- Reworked `SafeRefCount` (based on the rewrite by @hpvb)
- Replaced free atomic functions by the new `SafeNumeric<T>`
- Replaced wrong cases of `volatile bool` by the new `SafeFlag`
- Platform-specific implementations no longer needed

Co-authored-by: Hein-Pieter van Braam-Stewart <hp@tmm.cx>
2021-02-18 17:12:46 +01:00
PouleyKetchoupp cff7a69aa0 Fix test_body_motion recovery
This change makes test_body_motion more reliable when the kinematic body
recovers from being stuck.

- When recovery occurs, the rest information is generated, in order to
make sure collision results from test_move, move_and_collide and
move_and_slide are consistent and return a collision in case of overlap.

- The new calculation for recovery vector makes sure the recovery is
never more than the overlap depth between shapes.
This can help with cases where the kinematic body overlaps with several
shapes.
Recovery is made iteratively, without forcing a full overlap at each
step. This helps with getting proper rest information when recovery
occurs.

- One Way Collision:
When attempting motion, contact direction is checked against motion
before skipping in order to solve cases where kinematic bodies can sink
into one-way collision shapes.
Rest info now sets max contact depth in order to properly handle one-way
collision.

- Low speed motion is now handled in the rest info, by never setting
min_allowed_depth lower than motion length.
Separation is always applied with full margin, otherwise contact is lost
when low speed motion occurs right after higher speed motion.

- Similar changes are applied to 3D in order to make 2D and 3D
consistent.
2021-02-17 19:02:59 -07:00
reduz 8b19ffd810 Make Servers truly Thread Safe
-Rendering server now uses a split RID allocate/initialize internally, this allows generating RIDs immediately but initialization to happen later on the proper thread (as rendering APIs generally requiere to call on the right thread).
-RenderingServerWrapMT is no more, multithreading is done in RenderingServerDefault.
-Some functions like texture or mesh creation, when renderer supports it, can register and return immediately (so no waiting for server API to flush, and saving staging and command buffer memory).
-3D physics server changed to be made multithread friendly.
-Added PhysicsServer3DWrapMT to use 3D physics server from multiple threads.
-Disablet Bullet (too much effort to make multithread friendly, this needs to be fixed eventually).
2021-02-10 13:21:46 -03:00
PouleyKetchoupp 50501f2843 Fix physics queries not filtering out disabled collision shapes
This change allows collide_shape, intersect_shape, cast_motion and
rest_info in both 2D and 3D to ignore disabled shapes and make them
consistent with the physics simulation.

In some other cases, _cull_aabb_for_body is used and filters shapes out
internally, but whenever a physics query uses the broadphase directly
without calling _cull_aabb_for_body, disabled shapes can be returned
and need to be filtered out.
2021-02-09 19:14:17 -07:00
Rémi Verschelde d2e1216504
Merge pull request #37547 from aaronfranke/tau
Use Math_TAU and deg2rad/etc in more places and optimize code
2021-02-01 20:55:25 +01:00
Rémi Verschelde 5525cd85c6
Merge pull request #45315 from RandomShaper/modernize_thread
Modernize Thread
2021-01-31 15:24:56 +01:00
Pedro J. Estébanez 99fe462452 Modernize Thread
- Based on C++11's `thread` and `thread_local`
- No more need to allocate-deallocate or check for null
- No pointer anymore, just a member variable
- Platform-specific implementations no longer needed (except for the few cases of non-portable functions)
- Simpler for `NO_THREADS`
- Thread ids are now the same across platforms (main is 1; others follow)
2021-01-29 12:02:13 +01:00
Aaron Franke cb9fc117d1
Use real_t in physics code 2021-01-28 18:15:42 -05:00
Rémi Verschelde 011d201046
Merge pull request #43923 from madmiraal/fix-43588
Fix cast_motion sometimes failing
2021-01-14 20:44:19 +01:00
Aaron Franke 1d5042c9e2
Use Math_TAU and deg2rad/rad2deg in more places and optimize code 2021-01-09 03:47:14 -05:00
Marcel Admiraal 7653b8cc1e Fix multiple issues with one-way collisions
For RigidBodies, uses the collision normal determined by relative motion
to determine whether or not a one-way collision has occurred.

For KinematicBodies, performs additional checks to ensure a one-way
collision has occurred, and averages the recovery step over all collision
shapes.

Co-authored-by:    Sergej Gureev <sergej.gureev@relex.fi>
2021-01-07 09:22:39 +00:00
Rémi Verschelde b5334d14f7
Update copyright statements to 2021
Happy new year to the wonderful Godot community!

2020 has been a tough year for most of us personally, but a good year for
Godot development nonetheless with a huge amount of work done towards Godot
4.0 and great improvements backported to the long-lived 3.2 branch.

We've had close to 400 contributors to engine code this year, authoring near
7,000 commit! (And that's only for the `master` branch and for the engine code,
there's a lot more when counting docs, demos and other first-party repos.)

Here's to a great year 2021 for all Godot users 🎆
2021-01-01 20:19:21 +01:00
Rémi Verschelde 058f3fe069
Merge pull request #44149 from madmiraal/rename-tangent-orthogonal
Rename Vector2.tangent() to Vector2.orthogonal()
2020-12-28 16:00:12 +01:00
Marcel Admiraal 5b937d493f Rename empty() to is_empty() 2020-12-28 10:39:56 +00:00
Marcel Admiraal d7c3a41363 Remove all elements from monitored_bodies and monitored_areas when processed 2020-12-26 09:27:42 +00:00
Marcel Admiraal a24c38d1a8 Rename Vector2.tangent() to Vector2.orthogonal() 2020-12-06 18:16:06 +00:00
Jordan Schidlowsky 043b6c2d93 make 2d constraint solving more deterministic by solving in push order 2020-12-04 22:23:12 -06:00
Marcel Admiraal 356a2b7235 Fix cast_motion sometimes failing
- Fixes Godot physics failing when the cast Shape is inside of, or
already colliding with another Shape.
- Fixes Bullet physics failing when there is no motion.
- Ensures Godot and Bullet physics behave the same.
- Updates the documentation to exclude the caveats for the failures and
differences.
2020-11-27 15:33:00 +00:00
Aaron Franke 02161aad5a
Remove empty lines around braces with the formatting script 2020-11-16 23:38:11 -05:00
Rémi Verschelde d7176e9040
Merge pull request #42167 from madmiraal/fix-42108
Remove the unnecessary sync() and the restrictions it imposes on 3D Physics.
2020-11-16 09:32:48 +01:00
reduz 127458ed17 Reorganized core/ directory, it was too fatty already
-Removed FuncRef, since Callable makes it obsolete
-Removed int_types.h as its obsolete in c++11+
-Changed color names code
2020-11-07 20:17:12 -03:00
Marcel Admiraal 7e44682c03 Fix how Line2D obtains the other object's supports
Measure the distance from the line against the rotated object, not the
rotated line, when obtaining the object's supports against a line.
2020-10-03 12:59:34 +01:00
Fabio Alessandrelli f3c6ac1d71 Ignore thread models when compiling with NO_THREAD
The thread model option for physics (2D) and rendering (single-unsafe,
single-safe, multithread), was causing crashes/locks when set as
multithreaded and exported for a platform that does not support threads
(namely HTML5).

This commit ensures that when threads support is not available, that
option is ignored, and the equivalent of "single-unsafe" is always used
instead.
2020-09-23 11:24:01 +02:00
Marcel Admiraal 41209efa7f Remove the unnecessary sync() and the restrictions it imposes on 3D Physics. 2020-09-18 10:43:31 +01:00