Bitmap collisions, classes question

Understanding the language, error messages, etc.

Bitmap collisions, classes question

Postby DavidH » Sat Jan 03, 2015 12:48 am

Hi there! I'm trying to make a platformer, and I am having some trouble with bitmap collisions. I am relatively new to programming, and have one semester of computer science courses under my belt with a pretty thorough understanding of the basics of C++.
I got the Gamebuino for Christmas and have been looking through the references and codes of various games others have made. I started coding with classes for my first attempt and got some okay results. But after looking at other people's codes, I didn't see any classes, and I started to notice people using multiple .inos files. I did some more research on how those work, and I like it! I'm starting to get it more figured out, with using more global variables and stuff.

Anyways, I'm saying way too much without asking my question. The basic question is, would I be able to make a collision function that detects if a specific bitmap (i.e. the main character sprite) collides with a non-specific bitmap of which there are multiple instances (i.e. a floor tile or an enemy)? And how would I go about doing that?

The collideBitmapBitmap function seems to only take parameters of specific bitmaps, since it needs the x and y coordinates of both. Or maybe I just need to rethink how I initialize the bitmaps?

My "room" is a row of 4x4 tiles on the bottom of the screen. I made an array of their x-coordinates so that I could run through that with a for loop and the bitmap collide function, and that works fine because they all have the same y-coordinate. But as soon as I want to put a tile somewhere else, I obviously have a problem because I can't check that in the collide function. I just thought of a way to maybe do it with a 2D array, but this is getting a little too complicated. Any other ideas?

Sorry this is such a long "question". But I'm glad this community is here, it's pretty neat! Thanks for putting this all together, rodot!
DavidH
 
Posts: 1
Joined: Sat Jan 03, 2015 12:29 am

Re: Bitmap collisions, classes question

Postby rodot » Sat Jan 03, 2015 7:00 pm

Hello there and welcome aboard!
There are different ways to architecture your code :
- Only global variables and functions (quick and dirty but very messy in complex programs)
- structures and functions (a kind of compromise)
- Go full object oriented and use only classes (the "proper" way)

Usually, in the Arduino world, people go for the first option, as Arduino is for people who don't know much about programming. It's totally fine for simple programs, but get very messy when you try to make a fully featured game. You can tidy things up by putting your code in several tabs in the Arduino IDE (which are saved as several .ino files). The files are simply concatenated before compilation, so that only to makes it easier to navigate through your code. An example of this way of doing is Crabator : you can see it's very messy and difficult to understand (it's because it's started as a simple test and evolved into a game. I should re-factor it).

If you want you code to be more compact and efficient, you can create structures so you can reuse them. For example you can create a class "Rectangle", which can be used to define the mobs' hitbox or other thing as well. This way you can use only one function to process collision between rectangles, whether they belong to a mob, to the player, or to the level. The function are global functions which takes structures as arguments. An example of this way of coding would be "physics". Moreover this example could help you to understand how to create a function that checks the collision between one rectangle (the player) and several other rectangles (the map). If your games is tiled, then you should look at the game "UFO-race".

The "proper" way it to make everything object oriented like you've learned to do at school. Problem is that object oriented isn't fully supported on Atmel atmega chips (look for "arduino object oriented" in your favorite search engine). For examples there is no constructor or destructors (so people usually create a method named "begin()" which is used in place of the constructor).This is because there is no dynamic memory allocation on these chip. Everything has to be declared at compilation time.

You might consider using rectangle collision instead of bitmap collision as it's much faster.

That was my long answer to your long question. It's my vision of the things and might not be exact, but I hope it helps :)
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 13 guests

cron