-
Notifications
You must be signed in to change notification settings - Fork 78
Add object collision classification and resolution functions #197
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
Closed
Conversation
This file contains hidden or 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
8 tasks
…into pr/matt439/83
Add base64 image load function --------- Co-authored-by: Olivia McKeon <[email protected]> Co-authored-by: Sean Boettger <[email protected]>
…into pr/matt439/83
Add ray intersection functions for rectangles, circles, triangles, and quads --------- Co-authored-by: WhyPenguins <[email protected]>
Co-authored-by: @WhyPenguins
Contributor
|
Merged in thoth-tech |
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_directionandresolve_collision.calculate_collision_direction
calculate_collision_directiontakes 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_rectangleandrectangle_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_collisiontakes 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_directionto 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_directionandresolve_collisionhave 24 overloads each, covering all combinations ofsprite,rectangle,circle,triangle, andquad. The number of overloads is necessary as bothcalculate_collision_directionandresolve_collisionare 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
rectangleandcircle. 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_rectreturns false when thelineis contained within therectangle. This bug has been rectified.Type of change
How Has This Been Tested?
I altered
test_sprites.cppto 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_directionandresolve_collisionwill be called to resolve the collision. In addition, thecollision_directionis graphically shown on the player-controlled sprite as a red outline.I have also added a second sprite test to
test_sprites.cppwhich allows for complete testing of all 25 overloads.Keys
1to5change the collider object:Keys
6to-change the environment:6. Default environment where
classify_collision_directionis called beforeresolve_collision.7. The
collision_directionis fixed for eachspritein the cardinal directions.8. The
collision_directionis fixed for eachrectanglein the cardinal directions.9. The
collision_directionis fixed for eachcirclein the cardinal directions.0. The
collision_directionis fixed for eachtrianglein the cardinal directions.-. The
collision_directionis fixed for eachquadin the cardinal directions.The 3 new intersection functions and
line_intersects_recthave 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 bymakewhen in the/splashkit-core/projects/cmakedirectory. These additional tests will be merged with the other geometry unit tests in #196 as they use the same formatting.Testing Checklist
Checklist