Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a function which checks whether two sprites are undergoing pixel-level collision. Rather than bounding box collision in which the containing rectangles overlap, pixel level

Implement a function which checks whether two sprites are undergoing pixel-level collision. Rather than bounding box collision in which the containing rectangles overlap, pixel level collision occurs when (and only when) one or more opaque elements of the sprites occupy the same location in the screen frame of reference.

#include #include #include #include #include #include #include #include #include

// (Task 1) Define a function called pixel_level_collision which detects whether // two sprites s1 and s2 collide at pixel level. The function must be able // to work correctly even if the ZDK screen has not been set up, and should // make no attempt to use any function or variable declared in // , either directly or indirectly. // // Parameters: // s1 - the address of a Sprite which has been initialised and placed at // arbitrary position on the screen. // // s2 - the address of a Sprite which has been initialised and placed at // arbitrary position on the screen. // // Returns: // A boolean value which is true if and only if there exists a point (x,y) // which is opaque in both s1 and s2. // // Definition: (x,y) is opaque in sprite s if and only if: // 0 <= (sx = x - round(s->x)) < s->width, and // 0 <= (sy = y - round(s->y)) < s->height, and // offset(s,x,y) = sy * s->width + sx, and // s->bitmap[offset(s,x,y)] != ' '.

INSERT RETURN_TYPE FUNCTION_NAME ( PARAMETER_LIST ) { INSERT CODE HERE }

char * cross = "y y" /**/ " y y " /**/ " y " /**/ " y y " /**/ "y y" ; char * plus = " z " /**/ " z " /**/ "zzzzz" /**/ " z " /**/ " z " ;

int main( void ) { setup_screen(); sprite_id s_cross = sprite_create( 20.9, 8.3, 5, 5, cross ); sprite_id s_plus = sprite_create( 30.9, 17.9, 5, 5, plus );

while ( true ) { clear_screen(); sprite_draw( s_cross ); sprite_draw( s_plus ); if ( pixel_level_collision( s_cross, s_plus ) ) { draw_string(0, 0, "Pixel-level collision has been detected!"); } show_screen();

int key = wait_char();

if ( key == '2' ) { s_plus->y++; } else if ( key == '8' ) { s_plus->y--; } else if ( key == '4' ) { s_plus->x--; } else if ( key == '6' ) { s_plus->x++; } else if ( key == 'q' ) { break; } }

cleanup_screen();

return 0; }

Things to avoid

Do not attempt to draw the sprites within pixel_level_collision. This will cause bad things to happen.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions