-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add object collision classification and resolution functions #197
Open
matt439
wants to merge
26
commits into
splashkit:develop
Choose a base branch
from
matt439:collision-resolver
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matt439
changed the title
Collision resolver
Add object collision classification and resolution functions
Dec 13, 2024
8 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
While there are numerous functions to detect collisions in the SplashKit library, there are none which classify and resolve collisions between sprites, rectangles, circles, triangles, and quads. I have added two functions to the library:
calculate_collision_direction
andresolve_collision
.calculate_collision_direction
calculate_collision_direction
takes two objects and returns the enumerationcollision_direction
, indicating what part of the colliding object is being overlapped by the collidee object. It is not entirely accurate as it relies onsprite_collision_rectangle
andrectangle_around
. This is particularly true with rotated sprites and those using pixel collisions. However, it is robust and will handle any situation, including if one object is enclosed by the other.resolve_collision
resolve_collision
takes two objects and moves the collider so that they are no longer overlapping, without altering their velocities. The direction of collision is passed into the function. Student's are free to usecalculate_collision_direction
to obtain a direction, or their own methods. The function handles both AABB and pixel collisions. Pixel collisions are resolved iteratively, with the number of iterations being set byBRACKET_ITERATIONS
. A possible change would be to allow students to control the number of iterations, emphasizing the precision/performance tradeoff.Overloads
Both
calculate_collision_direction
andresolve_collision
have 24 overloads each, covering all combinations ofsprite
,rectangle
,circle
,triangle
, andquad
. The number of overloads is necessary as bothcalculate_collision_direction
andresolve_collision
are unidirectional functions where the order of the arguments is critical.Private Functions
I created many helper functions and an enumeration which are not included in
collisions.h
, as they should not be exposed to students.Code Duplication
With a large number of overloads, my goal was to minimise duplicated code. This proved difficult as there is no relationship between the object types such as
rectangle
andcircle
. It would have been much simpler if they all inherited from IShape and included some member functions. Instead, I made use of void pointers and downcasting to reduce duplicated code. This approach is not ideal as it is more complex and makes debugging more difficult. However, without major changes to the core SplashKit types, I believe it is the best approach.Additional Intersection Functions
I have added 3 new intersection functions to facilitate all collision combinations:
circle_quad_intersect
,rectangle_circle_intersect
, andtriangle_quad_intersect
.Bug Fix in line_intersects_rect
line_intersects_rect
returns false when theline
is contained within therectangle
. This bug has been rectified.Type of change
How Has This Been Tested?
I altered
test_sprites.cpp
to include two more sprites in the basic test: s3 uses pixel-based collision and s4 uses AABB collision. When the player-controlled sprite collides with s3 or s4,classify_collision_direction
andresolve_collision
will be called to resolve the collision. In addition, thecollision_direction
is graphically shown on the player-controlled sprite as a red outline.I have also added a second sprite test to
test_sprites.cpp
which allows for complete testing of all 25 overloads.Keys
1
to5
change the collider object:Keys
6
to-
change the environment:6. Default environment where
classify_collision_direction
is called beforeresolve_collision
.7. The
collision_direction
is fixed for eachsprite
in the cardinal directions.8. The
collision_direction
is fixed for eachrectangle
in the cardinal directions.9. The
collision_direction
is fixed for eachcircle
in the cardinal directions.0. The
collision_direction
is fixed for eachtriangle
in the cardinal directions.-. The
collision_direction
is fixed for eachquad
in the cardinal directions.The 3 new intersection functions and
line_intersects_rect
have been tested with unit tests. The tests are contained withinunit_tests_geometry.cpp
. Adding the new tests was achieved by runningcmake -G "Unix Makefiles" .
followed bymake
when in the/splashkit-core/projects/cmake
directory. These additional tests will be merged with the other geometry unit tests in #196 as they use the same formatting.Testing Checklist
Checklist