Advertisement

Spaceolopy

Started by July 25, 2022 01:09 AM
86 comments, last by pbivens67 1 year, 11 months ago

pbivens67 said:
ok well how do I do two or more boxes?

You make one box for each bug you already have.
You make one box for each bullet you already have.
Then, for each bullet, you loop over all bugs to see if they intersect, and kill them. \:D/

You could create those boxes just temporarily for the intersection test, using your x, y, width and height variables.
Or you could replace those variables in your bug and bullet classes with a box.

Many options, already discussed. Just try something out, see how it goes, and come back with a precise problem of your approach, in case there is one.

Idk what's the state of your game, but i guess you have 5 x 8 bugs on screen already? And some moving bullet? From there it shouldn't be hard to add some collision response.

well, are there any good tutorials on AABB collision detection with multiple boxes?

Advertisement

well I found some code that might work for my purpose, but the “one” variable gives an “expression must have a class type" error, I goggled the error but can't seem to figure it out

#include <windows.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class GameObject
{
public:
	int x;
	int y;
	int Position;
	int Size;
};
bool CheckCollision(GameObject &one, GameObject &two)// AABB - AABB collision
{
	// collision x-axis?
	bool collisionX = one.Position.x + one.Size.x >= two.Position.x && two.Position.x + two.Size.x >= one.Position.x;
	// collision y-axis?
	bool collisionY = one.Position.y + one.Size.y >= two.Position.y && two.Position.y + two.Size.y >= one.Position.y;
	// collision only if on both axes
	return collisionX && collisionY;
}

int main()
{
	GameObject myGameObject;
	return 0;
}   

pbivens67 said:
well, are there any good tutorials on AABB collision detection with multiple boxes?

You already had a first function, which looked like working.
Then you added a second way, by adopting my example of using a AABox class, which now should work.

A tutorial can be about two things: 1. The math and logic of an intersection test 2. potential solutions an the many bodies problem. That was all covered here.

What exactly is your problem so now you work on a third way to do this intersection test?
The issue is this:

pbivens67 said:
// collision x-axis? bool collisionX = one.Position.x +

Position has a x member variable, so likely they use a 2D vector class for their position. Exactly the same as with my AABox class, which used vec2 as well. And you have correctly replaced this with an array of two floats.

But here you did this:

pbivens67 said:
class GameObject { public: int x; int y; int Position;

You have x and y. Which is position.
But you also have a one dimensional Position variable as well, which can't describe the position in 2D space needed for a box.

So you either need to use x and y, changing the intersection code accordingly.
Or you have to make the Position variable a float Position[2], and replace .x and .y of the intersection code with array indices.

Or you spare all this redundant work, which you already did before two times. Because you already have two intersection methods which both should work.

I can help you, but for that you need to answer my questions:

JoeJ said:
Idk what's the state of your game, but i guess you have 5 x 8 bugs on screen already? And some moving bullet? From there it shouldn't be hard to add some collision response.

My intend here is: You show what you have, then i can propose how to add collision response.

Basically i need to see how you create your bugs, what's their data structures and related code.

JoeJ said:
Idk what's the state of your game, but i guess you have 5 x 8 bugs on screen already?

you are right but there has to be an easy and efficient way to kill the bugs I have already drawn; I can kill two bugs which is easy but to kill 40 bugs is quite difficult. here is my code to kill two bugs.

void coll_ship_one()
{
	//draw bullet
	float x = -10.0f + move_x;
	float y = -90.0f + bullet;
	float oWidth = 5.0f;
	float oHeight = 5.0f;
	//draw bug
	float xTwo = -10.0f;
	float yTwo = 90.0f;
	float oTwoWidth = 10.0f;
	float oTwoHeight = 10.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		coll = 1;
		coll_count++;
		if (coll_count >= 1)
		{
			coll_count = 1;
		}
		cout << coll << " " << coll_count << endl;
		glutPostRedisplay();
	}
}

void coll_ship_two()
{
	//draw bullet
	float x = -10.0f + move_x;
	float y = -90.0f + bullet;
	float oWidth = 5.0f;
	float oHeight = 5.0f;
	//draw bug
	float xTwo = -30.0f;
	float yTwo = 90.0f;
	float oTwoWidth = 10.0f;
	float oTwoHeight = 10.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		coll = 1;
		coll_count++;
		if (coll_count >= 1)
		{
			coll_count = 1;
		}
		cout << coll << " " << coll_count << endl;
		glutPostRedisplay();
	}
}

@JoeJ can you reply to the above

Advertisement

Damn!

I have had written half of the game code already, then clicked some stupid refresh button and all is lost >:( why did it do this? I never click refresh while writing a post…

Ok. Will do again, but later.

Looking at your code it's clear you run into limitations because you do not use data structures.
Instead you're used to generate data procedurally with inside the code.
Collision tests is not really the problem. Just by coincidence it's the point where you actually get to the limits on what's possible with your bad coding habits.

We discussed this before. We called it Sprites, or GameObjects, etc. That's actually data structures. They are essential.

Will post an example later…

ok I will brush on my data structures, like lists and vectors

pbivens67 said:
like lists and vectors

The point is: You have to come up with YOUR OWN DATA STRUCTURES!!!

lists and vectors are just containers, usually used to manage your own data structures ; )

@JoeJ then what is a data structure?

This topic is closed to new replies.

Advertisement