# How do I make a solid object

I’m trying to make a solid object but I don’t know how😕

There are a great many resources for this on the web.

Typically you’ll be wanting to do rectangle collisions, like outlined in this stackoverflow question.

This mozilla article specifies how to check for collisions with rectangles and circles, as well as the more complicated separating axis theorem used for polygon collisions and discusses how to improve collision checking performance.

Note that the Arduboy2 library has some built in collision functions.

Otherwise you can always disect existing games, I’m sure most people here won’t mind you reading their code to learn from it.

4 Likes

If you haven’t already, I suggest you work your way through @crait’s tutorials and make sure you understand each one. By the time you finish, you should have an idea of ways to create and control “solid objects”.

5 Likes

None of them really helped including @pharap 's.
Can someone explain it in their own words?

There aren’t​ solid objects to be created, everything is conceptual. If you draw something and make the player position impossible to go thru that drawing, it will seem like a solid object. But you have to design that.

For my game, I just say the position is invalid if it collides with the road block: https://github.com/eried/ArduboyBackToTheJungle/blob/master/back2jungle/game.ino#L78

1 Like

I’m not sure what this means could you explain?

Basically it’s all about checking for intersections.

Say for example you’re using rectangles, your player character has a rectangle defining its boundary and your walls have rectangles defining their boundaries.
You can check if two rectangles intersect each other, which tells you when a character has walked into a wall, but it’s up to you how you resolve that. You can choose to just move your character back to a previous position or if you’re simulating physics you can change your character’s velocity to make them bounce off the wall.

It might even depend on what you’re trying to achieve. For example:

• A player instersects a wall - you want to move the player back to before they were walking through the wall.
• A bullet intersects a player - you want to remove the bullet and damage the player.
• A bullet intersects a wall - you want to remove the bullet and either do nothing to the wall or make it look damaged.
• A pinball hits a bumper - you want to make the pinball bounce off the bumper and maybe play a sound.

In the case of having to move something backwards, this means you calculate where things are going to move to before moving them, then check the new location for collisions, and only if the place is collision free do you move the thing.

This is what @eried is doing, but the code he quoted probably isn’t the best example. From the same game, look at this code:

``````// Check collision
if (arduboy.collide({(byte)playerx + 2, (byte)playery + 2, (byte)(player.width - 4), (byte)(player.height - 4)},
{enemies[e].x + 2, (byte)(enemiesPos[enemy] + 2), (byte)(specs.width - 4), (byte)(specs.height - 4)}))
{
doCrash();
exitCode = LOSE;
}
``````

Here he is checking if the player’s rectangle intersects the enemy’s rectangle and if they are intersecting, he changes the result of the game step to `LOSE` to signify that the player has lost a life. Some code elsewhere decides whether the player has enough lives left to continue.

4 Likes

Nope.
I still don’t understand can you put it in this

#include "Arduboy2.h" Arduboy2 arduboy; Void setup() { } Void loop() { }

I think what you are asking is out of your skill set for now.

Start with @crait tutorials Make Your Own Arduboy Game: Part 1 - Setting Up Your Computer

5 Likes

There is no simple copy and paste solution that will work for every game.
Like I said before, it depends on what kind of collision you’re trying to do.

Here’s a demonstration of how the `Arduboy2` library’s `collide` function works, but this only demonstrates how to check that the two rectangles are colliding, it doesn’t make any attempt to handle the collision, that’s something that differs between games.

``````#include <Arduboy2.h>
Arduboy2 arduboy;

Rect rectA = {0, 0, 16, 16};
Rect rectB = {(WIDTH/2) - (16/2), (HEIGHT/2) - (16/2), 16, 16};

void setup()
{
arduboy.begin();
}

void loop()
{
if(!arduboy.nextFrame())
return;

if(arduboy.pressed(LEFT_BUTTON)) { --rectA.x; }
if(arduboy.pressed(RIGHT_BUTTON)) { ++rectA.x; }
if(arduboy.pressed(UP_BUTTON)) { --rectA.y; }
if(arduboy.pressed(DOWN_BUTTON)) { ++rectA.y; }

arduboy.clear();
arduboy.fillRect(rectA.x, rectA.y, rectA.width, rectA.height);
arduboy.fillRect(rectB.x, rectB.y, rectB.width, rectB.height);
arduboy.setCursor(0, 56);
if(arduboy.collide(rectA, rectB))
{
arduboy.print(F("Colliding"));
}
else
{
arduboy.print(F("Not Colliding"));
}
arduboy.display();
}
``````
5 Likes

(Yes, I like to nitpick )

That’s the same as just `WIDTH/2`
And obviously the same goes for `HEIGHT - (HEIGHT/2)`.

1 Like

Cut me some slack I literally threw it together in 5 minutes whilst writing several other things.

Heck, I’m amazed it compiles, or that I remembered `F`.

(Plus it’s 29C in here and I’ve been up for 16 hours.)
(Halp, I can’t stop writing code D:)

3 Likes

Cody, you’ve demonstrated that you’re actually learning a lot from the Arduboy and that’s great. But, you’re thinking about programming in terms of physical reality, instead of virtual reality. There is no such thing as solid objects in computer games. Computer games make you think things are solid by how they interact with each other. The computer (or game system) will realize that two things are touching and prevent them from moving towards each other. With that in mind, if you’re making a game and you want something to seem solid, what you need to do is check to see if it is touching anything. To do that, you check to see if anything else is being drawn or has the same location.

Now, I don’t expect you to completely understand how to program solid objects just from that explanation, but keeping that in mind, the Pong tutorial that I wrote gives a great example of checking for collision. If the ball is touching the paddles, it’ll flip direction. From an abstract perspective, those paddles are solid. I’d suggest going through the tutorials and reading up to the Pong tutorial so you make sure to understand it all.

4 Likes

Virtual reality -> numbers and logic instead of matter.