'// GDK Demos - Collision detection
'// For GDK 1.7
'// Root file path
CHDIR "Unseen Demos\Resources\"
'// Now we will be using two sprites, for ease i've used an array
'// Each sprite has an associated Vector and a Rectangle
'// As the enemy sprite is animated it also has an Anim array
DIM Sprite
(1) AS Sprite
, Position
(1) AS Vector
, Rect
(1) AS Rectangle
, Anim
AS ANIMATION
'// Input handler
'// Variable for demo - To show collision rectangles
ViewRect% = -1
'// Screen
GDK_Screen_SetRes Main&, 800, 600
'// Load the sprites
GDK_Sprite_New Sprite(0), "Ship1.PNG", 1, 1, 1, 1
GDK_Sprite_New Sprite(1), "Invader1.PNG", 4, 1, 4, 1
'// Set the sprites up (Visibility, Alpha and rotation point)
GDK_Sprite_Show Sprite(i%)
GDK_Sprite_SetAlpha Sprite
(i%
), _RGB32(255, 0, 255) GDK_Sprite_SetRotationAsCenter Sprite(i%)
'// Set initial Vector values (position, rotation and speed)
'// ** NOTE - GDK uses Radians to handle rotation values (0 to 2 * PI), Increasing turns CCW, Decreasing CW
Position(0).X = 400
Position(0).Y = 300
Position(1).X = 400
Position(1).Y = 200
Position(1).Speed = 2
Position
(1).Rotation
= 3 * ATN(1)
'// Set up enemy animation
GDK_ANIMATION_NEW Anim
, 1, 1, 4, .2, TIMER(.001)
'// Value to increase enemy rotation by
EnemyRotInc!
= (4 * ATN(1)) / 300
'// Main loop
'// User input
GDK_Keyboard_GetState KB
Position(0).Rotation = Position(0).Rotation + .0314
Position(0).Rotation = Position(0).Rotation - .0314
IF Position
(0).Speed
< 5 THEN Position
(0).Speed
= Position
(0).Speed
+ .2 IF Position
(0).Speed
> -1 THEN Position
(0).Speed
= Position
(0).Speed
- .2
'// Turn visable rectangles on/off
'// Increase enemy rotation
Position(1).Rotation = Position(1).Rotation + EnemyRotInc!
'// Loop through the sprite array and do stuff
'// Update the position of each sprite's vector
GDK_Vector_Update Position(i%)
'' Store the scale value, change it then put it back
OldScale! = Sprite(i%).Scale
Sprite(i%).Scale = 80 * (Sprite(i%).Scale / 100)
'// Autosize the sprites collision rectangle
'// Thanks to Richard Notely the rectangles actually rotate with the image!
GDK_Rectangle_Autosize Sprite(i%), Position(i%), Rect(i%)
'// reset scale value
Sprite(i%).Scale = OldScale!
'// Update animation
GDK_ANIMATION_UPDATE Anim
'// Draw the sprite's
IF i%
= 0 THEN Frame%
= 1 ELSE Frame%
= Anim.Frame
GDK_Sprite_Draw Sprite(i%), Position(i%), Frame%
'// DEMO CODE - To show actual collision rectangles
LINE (Rect
(i%
).C1.X
, Rect
(i%
).C1.Y
)-(Rect
(i%
).C2.X
, Rect
(i%
).C2.Y
), _RGB32(255, 0, 0) LINE (Rect
(i%
).C2.X
, Rect
(i%
).C2.Y
)-(Rect
(i%
).C3.X
, Rect
(i%
).C3.Y
), _RGB32(255, 0, 0) LINE (Rect
(i%
).C3.X
, Rect
(i%
).C3.Y
)-(Rect
(i%
).C4.X
, Rect
(i%
).C4.Y
), _RGB32(255, 0, 0) LINE (Rect
(i%
).C4.X
, Rect
(i%
).C4.Y
)-(Rect
(i%
).C1.X
, Rect
(i%
).C1.Y
), _RGB32(255, 0, 0)
'// Check for collision
'// Library inclusion
REM $INCLUDE:'Unseengdk.bm'