forked from splashkit/splashkit-core
-
Notifications
You must be signed in to change notification settings - Fork 41
Improve sprite_bitmap_collision Cell Parameter Documentation #141
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
Open
ralphweng2023
wants to merge
5
commits into
thoth-tech:main
Choose a base branch
from
ralphweng2023:improve/sprite-collision-cell-docs
base: main
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.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00d51df
Improve documentation for sprite_bitmap_collision cell parameter
ralphweng-autograb bc0f108
Merge upstream/main into improve/sprite-collision-cell-docs
ralphweng-autograb 10ee6d4
Address code review feedback
ralphweng-autograb f8f5625
Fix collision mask not working for created bitmaps
ralphweng-autograb 8a442c9
Fix collision test: remove clear_bitmap with COLOR_TRANSPARENT
ralphweng-autograb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // | ||
| // test_sprite_bitmap_cell_collision.cpp | ||
| // splashkit | ||
| // | ||
| // Example demonstrating sprite-bitmap collision detection using cells. | ||
| // This shows how to check collision against specific animation frames | ||
| // within a sprite sheet rather than the entire bitmap. | ||
| // | ||
|
|
||
| #include "sprites.h" | ||
| #include "images.h" | ||
| #include "graphics.h" | ||
| #include "window_manager.h" | ||
| #include "input.h" | ||
| #include "collisions.h" | ||
| #include "text.h" | ||
| #include <iostream> | ||
|
|
||
| using namespace std; | ||
| using namespace splashkit_lib; | ||
|
|
||
| void run_sprite_bitmap_cell_collision_test() | ||
| { | ||
| open_window("Sprite Bitmap Cell Collision Test", 800, 600); | ||
|
|
||
| // Load a sprite sheet with multiple frames representing an environmental hazard | ||
| // In this example, we'll create a bitmap representing a spike trap with different states | ||
| // Frame 0: Retracted (no spikes - safe) | ||
| // Frame 1: Extending (medium spikes - some danger) | ||
| // Frame 2: Fully Extended (large spikes - dangerous) | ||
| bitmap hazard_spritesheet = create_bitmap("spike_trap_sheet", 300, 100); | ||
|
|
||
| // Set up cell details: 3 frames (100x100 each) in a horizontal strip | ||
| bitmap_set_cell_details(hazard_spritesheet, 100, 100, 3, 1, 3); | ||
|
|
||
| // Draw different spike states in each cell with TRANSPARENT backgrounds | ||
| // This is crucial - the background must be transparent for proper collision detection | ||
| clear_bitmap(hazard_spritesheet, COLOR_TRANSPARENT); | ||
|
|
||
| // Frame 0 (Retracted): Small grey square (20x20) at bottom - minimal collision area | ||
| fill_rectangle_on_bitmap(hazard_spritesheet, rgba_color(128, 128, 128, 255), 40, 75, 20, 20); | ||
|
|
||
| // Frame 1 (Extending): Medium red trapezoid shape (40x40) - moderate collision area | ||
| // Draw a simple trapezoid to represent extending spikes | ||
| fill_rectangle_on_bitmap(hazard_spritesheet, rgba_color(200, 0, 0, 255), 130, 55, 40, 40); | ||
| fill_triangle_on_bitmap(hazard_spritesheet, rgba_color(200, 0, 0, 255), | ||
| 130, 55, 170, 55, 150, 40); | ||
|
|
||
| // Frame 2 (Extended): Large bright red spike pattern (60x70) - maximum collision area | ||
| // Draw multiple triangular shapes to represent fully extended spikes | ||
| // Base platform | ||
| fill_rectangle_on_bitmap(hazard_spritesheet, rgba_color(150, 0, 0, 255), 220, 75, 60, 20); | ||
| // Spikes pointing up | ||
| fill_triangle_on_bitmap(hazard_spritesheet, rgba_color(255, 0, 0, 255), | ||
| 220, 75, 230, 75, 225, 25); | ||
| fill_triangle_on_bitmap(hazard_spritesheet, rgba_color(255, 0, 0, 255), | ||
| 235, 75, 245, 75, 240, 25); | ||
| fill_triangle_on_bitmap(hazard_spritesheet, rgba_color(255, 0, 0, 255), | ||
| 250, 75, 260, 75, 255, 25); | ||
| fill_triangle_on_bitmap(hazard_spritesheet, rgba_color(255, 0, 0, 255), | ||
| 265, 75, 275, 75, 270, 25); | ||
|
|
||
| // IMPORTANT: Setup collision mask for the hazard bitmap | ||
| // This ensures transparent areas are NOT counted in collision detection | ||
| setup_collision_mask(hazard_spritesheet); | ||
|
|
||
| // Create a player sprite with a solid circle | ||
| bitmap player_bmp = create_bitmap("player", 40, 40); | ||
| clear_bitmap(player_bmp, COLOR_TRANSPARENT); | ||
| fill_circle_on_bitmap(player_bmp, COLOR_YELLOW, 20, 20, 18); | ||
|
|
||
| // Setup collision mask for player too | ||
| setup_collision_mask(player_bmp); | ||
|
|
||
| sprite player = create_sprite("player", player_bmp); | ||
| sprite_set_x(player, 400); | ||
| sprite_set_y(player, 300); | ||
|
|
||
| // Hazard bitmap position (fixed on screen) | ||
| double hazard_x = 350; | ||
| double hazard_y = 250; | ||
|
|
||
| int current_cell = 0; // Start with retracted spikes | ||
|
|
||
| cout << "Sprite Bitmap Cell Collision Test" << endl; | ||
| cout << "Use LEFT/RIGHT arrows to change spike trap animation frame (cell)" << endl; | ||
| cout << "Use WASD to move the player sprite" << endl; | ||
| cout << "Observe how collision changes with different cells!" << endl; | ||
| cout << "The trap cycles through: Retracted -> Extending -> Extended" << endl; | ||
|
|
||
| while (!quit_requested()) | ||
| { | ||
| process_events(); | ||
|
|
||
| // Handle input for changing cells | ||
| if (key_typed(RIGHT_KEY)) | ||
| { | ||
| current_cell = (current_cell + 1) % 3; | ||
| } | ||
| if (key_typed(LEFT_KEY)) | ||
| { | ||
| current_cell = (current_cell - 1 + 3) % 3; | ||
| } | ||
|
|
||
| // Move player with WASD | ||
| if (key_down(W_KEY)) sprite_set_y(player, sprite_y(player) - 3); | ||
| if (key_down(S_KEY)) sprite_set_y(player, sprite_y(player) + 3); | ||
| if (key_down(A_KEY)) sprite_set_x(player, sprite_x(player) - 3); | ||
| if (key_down(D_KEY)) sprite_set_x(player, sprite_x(player) + 3); | ||
|
|
||
| // Check collision with the SPECIFIC CELL (frame) of the hazard sprite sheet | ||
| // This is the key function we're demonstrating! | ||
| bool colliding = sprite_bitmap_collision(player, hazard_spritesheet, current_cell, hazard_x, hazard_y); | ||
|
|
||
| // Draw | ||
| clear_screen(COLOR_WHITE); | ||
|
|
||
| // Draw the current cell of the hazard sprite sheet | ||
| drawing_options opts = option_part_bmp( | ||
| current_cell * 100, // x offset in source | ||
| 0, // y offset in source | ||
| 100, // width | ||
| 100 // height | ||
| ); | ||
| draw_bitmap(hazard_spritesheet, hazard_x, hazard_y, opts); | ||
|
|
||
| // Draw a dashed rectangle around the hazard to show cell boundary | ||
| draw_rectangle(rgba_color(0, 0, 0, 100), hazard_x, hazard_y, 100, 100); | ||
|
|
||
| // Draw the player sprite (colour changes based on collision) | ||
| if (colliding) | ||
| { | ||
| // Player is colliding - draw in red with pulsing effect | ||
| fill_circle(rgba_color(255, 0, 0, 200), sprite_x(player) + 20, sprite_y(player) + 20, 20); | ||
| // Draw warning outline | ||
| draw_circle(COLOR_RED, sprite_x(player) + 20, sprite_y(player) + 20, 25); | ||
| } | ||
| else | ||
| { | ||
| // No collision - draw normally | ||
| draw_sprite(player); | ||
| } | ||
|
|
||
| // Display information with better formatting | ||
| string cell_names[] = {"Retracted (safe)", "Extending (danger!)", "Extended (deadly!)"}; | ||
| color cell_colors[] = {COLOR_GREEN, rgba_color(255, 140, 0, 255), COLOR_RED}; | ||
|
|
||
| // Title | ||
| draw_text("Spike Trap Cell Collision Demo", COLOR_BLACK, "Arial", 20, 10, 10); | ||
|
|
||
| // Current state info | ||
| draw_text("Current Trap State: " + cell_names[current_cell], cell_colors[current_cell], "Arial", 16, 10, 40); | ||
| draw_text("Cell Index: " + to_string(current_cell), COLOR_BLACK, 10, 60); | ||
|
|
||
| // Collision status with prominent display | ||
| string collision_text = colliding ? "COLLISION DETECTED!" : "No Collision"; | ||
| color collision_color = colliding ? COLOR_RED : COLOR_GREEN; | ||
| draw_text(collision_text, collision_color, "Arial", 18, 10, 90); | ||
|
|
||
| // Instructions box | ||
| fill_rectangle(rgba_color(240, 240, 240, 255), 10, 480, 380, 110); | ||
| draw_rectangle(COLOR_BLACK, 10, 480, 380, 110); | ||
| draw_text("Instructions:", COLOR_BLACK, "Arial", 14, 20, 490); | ||
| draw_text("LEFT/RIGHT: Change spike trap animation state", COLOR_BLACK, 20, 510); | ||
| draw_text("WASD: Move player", COLOR_BLACK, 20, 530); | ||
| draw_text("Move close to see collision detection in action!", COLOR_BLACK, 20, 550); | ||
|
|
||
| // Explanation box | ||
| fill_rectangle(rgba_color(230, 240, 255, 255), 400, 480, 390, 110); | ||
| draw_rectangle(COLOR_BLUE, 400, 480, 390, 110); | ||
| draw_text("What This Demonstrates:", COLOR_BLUE, "Arial", 14, 410, 490); | ||
| draw_text("Testing collision with SPECIFIC CELLS (frames)", COLOR_BLACK, 410, 510); | ||
| draw_text("Different animation states = different collision areas", COLOR_BLACK, 410, 530); | ||
| draw_text("Notice: Retracted trap has tiny hitbox, extended has large hitbox!", COLOR_BLACK, 410, 550); | ||
|
|
||
| refresh_screen(60); | ||
| } | ||
|
|
||
| free_sprite(player); | ||
| free_bitmap(player_bmp); | ||
| free_bitmap(hazard_spritesheet); | ||
| close_all_windows(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest removing this line - this solved the issue on my end.
COLOR_TRANSPARENTseems to be the problem - this colour is supposed to have an alpha value of 0, but my testing showed an alpha value of 255, which is probably affecting the collision mask on areas supposed to be transparent. This explains why loaded bitmaps were not affected - they didn't have this color applied.For reference, I used this program to test the alpha value: