Page 1 of 1

Problem with Collision Detection

PostPosted: Sat Feb 11, 2017 7:34 pm
by reuta
Hello Everyone!

I am new here. I got my Gamebuino for christmas.
I was trying to program sort of a "duckhunt" game, where you need to shoot ducks.
There's a cross on the screen which the player can move to aim at the ducks.
I am currently only playing around, so ducks are just some small rectangles.
My code might be a bit messy. I am not an experienced programmer ( I am 15 years old going to high school).
A problem I encountered, was that when i pressed k to shoot, I can't kill the ducks. But I am aiming at them.
I think this is because i am doing something wrong with collision detection.
I tested everything with the web based gamebuino emulator.

Here's my code pasted: http://pastebin.com/raw/8T8JUcM0

Thank you in advance!

Adrian

Re: Problem with Collision Detection

PostPosted: Sun Feb 12, 2017 12:25 pm
by wuuff
The byte data type is unsigned, which meant that when you assigned it a random value from -1 to 1, you were actually assigning it a value of 255, 0, or 1. It only took a few loops for the x position of the ducks to reach a few thousand. This looked normal because when the Gamebuino draws to the screen it uses unsigned bytes, and trying to draw something far off the screen causes those values to overflow, meaning it still drew the ducks in a place that looked right, even though their actual coordinates were in the thousands.

Try changing "byte dir;"in struct Duck to "char dir;". That should solve the problem. If you don't want to use char, you can also use "int8_t".

Your code is plenty clean and easy to understand. I would say it's quite good for a 15-year-old in high school. Good luck with your Gamebuino projects, and I hope you post some cool games in the future!

Re: Problem with Collision Detection

PostPosted: Mon Feb 13, 2017 3:29 pm
by reuta
Thank you! This solved my problem.