I'm making collision detection in a game, does this look right to you?

I'm making collision detection in a game, does this look right to you?

Attached: Untitled.png (648x589, 29K)

Other urls found in this thread:

r-5.org/files/books/computers/algo-list/realtime-3d/Christer_Ericson-Real-Time_Collision_Detection-EN.pdf
stackoverflow.com/questions/41946007/efficient-and-well-explained-implementation-of-a-quadtree-for-2d-collision-det
twitter.com/AnonBabble

Something tells me you could return false; early in a lot of easily detected situations.

your function is called "hitboxesIntersect" yet it appears to be doing something completely different involving raycasting

Hmm what do you means?

I'm not sure what to call it. It doesn't neceserrily directly concern the Hitbox class to be a virtual function, but I need an easy to look-up name. What do you recommend

This is what I implemented to detect boxes in 2D which is working but ugly af:
int check_collision( SDL_Rect &A, SDL_Rect &B ) {

//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA,topB;
int bottomA, bottomB;
//Go through the A boxes
for( int Abox = 0; Abox < A.size(); Abox++ ) {
//Calculate the sides of rect A
leftA = A[ Abox ].x;
rightA = A[ Abox ].x + A[ Abox ].w;
topA = A[ Abox ].y;
bottomA = A[ Abox ].y + A[ Abox ].h;
//Go through the B boxes
for( int Bbox = 0; Bbox < B.size(); Bbox++ ) {
//Calculate the sides of rect B
leftB = B[ Bbox ].x;
rightB = B[ Bbox ].x + B[ Bbox ].w;
topB = B[ Bbox ].y;
bottomB = B[ Bbox ].y + B[ Bbox ].h;
//If no sides from A are outside of B
if( ( ( bottomA = bottomB ) || ( rightA = rightB ) ) == false ) {
// A collision is detected
return true;
}
}
}
//If neither set of collision boxes touched
return false;
}

are you trying to test if two axis-aligned boxes are intersecting?

Why the fuck would I know by looking at 10 lines of code with at least 50 undefined things inside. If the function is written with some background it might be correct, if it's some hacked code taken from some tutorial it might not.
Instead of letting Jow Forums speculatively debugging your code why don't you try executing it and see if it actually works?

Very good but I am not yousing a rectangle. I have a rectangle, a circle, and anything else that can define its shape procedurally by Point containment.
It works flawlessly and it is so hilariously faste. I love how fast it is I can calculate 50 THOUSAND circle intersections with 5 units diameter in no perceptible time.

>accessor functions like xPos() instead of just getting the member variable
What's the point of using C++ if you're going to do slow shit like that?

"box" implies rectangle. "hitbox" is a stupid consumer term for any collision shape, but if you're a programmer you should be more precise. That said your method is an inelegant brute-force approach and you should probably look up the algorithms for detecting collisions between different shapes, or use a library

On your mind, do you not think C++ is made for encapsulation?

>Very good but I am not yousing a rectangle. I have a rectangle, a circle, and anything else that can define its shape procedurally by Point containment.
You may want to define the shapes so that each has a max bound that can be represented as an AABB, a sphere or something, and immediately return false if the boxes (or balls) aren't touching.

Bingo! That is genius! I was just banging my head how to handle spawning new objects in the game correctly.
So if I want to emulate a ship taking a shot, I spawn a new Shell entity which if offset past the max point of its source?
That's what I am wondering, how to actually handle an entity shooting. Do I associate an entity with its container and go Container.addObj(new Shell(...))?

>I can calculate 50 THOUSAND circle intersections with 5 units diameter
your collision algorithm gets slower the larger the shapes are
that makes it pretty unflexible
it's also imprecise seeing it works in steps instead of absolutes

So I basically if the other object's max point toward this centre is inside this object and vice versa, they intersect?

Attached: Untitled.png (306x264, 3K)

there's no one formula for the intersection of different shapes. there's a different algorithm for each shape combination. If you're just using rectangles and circles it's pretty simple, I suggest you look them up

Thanks

Jow Forums is not qualified enough for games. Try /vg/'s /agdg/:

No. If you know the shape of your hitbox it should be significantly easier to check whether they intersect.
Assuming your "hitbox" is a 1D line one "if" is enough, just check if the abs of the differences in the center is smaller or larger then half the lengths.
Same can be done in n-D with more "if"s.
Mixed shapes might be tricky, but should be possible too.

>there's no one formula for the intersection of different shapes
There is, mathematically speaking.

>There is, mathematically speaking.
not sure what kind of smartass reply you're making but in practise you use a different algorithm for each shape pair, some are more general for others, like it's faster to use an AABB vs AABB check than a more general convex vs convex check

The compiler cannot inline a simple getter?

don't count on compilers to inline your code for you

>return false if balls aren't touching
bool isGay(Situation s, Ball[] balls)

>C++ game
>use inheritance

lmao@ur life

That's a pretty stupid fucking way to detect collision.
You can just do it analytically based on the shapes.
What kind of shapes do you have?

> What kind of shapes do you have?
huge dicks and tight assholes.

Thanks for the help you guys are precious

I've ltierally no clue what the fuck I'm doing and what the fuck is going on, I'm just trying to make this object-oriented following Game Design Patterns book. Always feels like I could do it in C in 500 lines.
Does this class look like something you'd hate or like to see?

Attached: Untitled.png (841x703, 37K)

just use Unity bruh

I can't. I havve to make a real life project to pass my OOP class (3rd year failed).

You should remove the gets and sets parts of you member function names. Only a brainlet wouldn't understand what "if (IsShooting())" means

it's an alright convention for getters and setters. only brainlets would focus on such superfluous things and not actual code

I prefer
bool shooting;
void setShooting(bool shooting);
bool isShooting();

seems fine if you are going for a bullet hell project for a class
the raw pointers hurt tho
also dont forget the virtual destructor if you have other classes inheriting from Entity
shooting probably deserves its own component imo

>the raw pointers hurt tho
I don't see any reason why. An Entity does not own any of those other objects.

Thanks fellars.
I think I finally got the hang of this and the purpose of components, it seems so obvious now.
Of course! Each component does its own thing and I just supply the prerequisites.

What owns them then? Doesn't each Entity have it's own component instances that should get destroyed along with it?

No mate I've abstracted that away. I am writing a class factory as we speak.

read this nigga
r-5.org/files/books/computers/algo-list/realtime-3d/Christer_Ericson-Real-Time_Collision_Detection-EN.pdf

Looks interesting OP.
also looks like you'd want to settle for a set shape of hitbox and compose everything from it, either rectangles or circles. Computing collisions between them should be simple.
A stylistic note - consider simplifying and clarifying your code. It's a bit verbose but it won't hurt anyone if you write "leftHandSide" rather than "lhs" and a few less internal variables if you can avoid them. Complicated code is hard to debug (or to return to after 3 months)

Thanks. Prof got mad for me using just rectangles and asked me to add ellyptic hitboxes on the spot if "I'm as hot as I think" haha then I failed

Add elliptic hitboxes? not substitute? then rect X ellipse, rect X rect, ellipse X ellipse? man fuck your prof
Should've used that abstract hitbox class

How, exactly, do you plan on running this?
Between every single object available every frame?

>Gamemaker Studio: The Thread

>get
>set
>get
>set
>not doing
>bool IsShooting() const;
>void IsShooting(bool);
Disgusting.

Whenever I scan the array for collisions

Also look into using a quadtree - if it's a bullet hell or something and there are a ton of collisions, it's worth looking into.

I don't know trees or anything like that. I forgot all about them. Where do I read?

So, yes?
If you're using regular hitboxes then you can do this with a couple coordinate points, particularly if the hitboxes are of known sizes, you don't need to lerp a ray through them.
Same thing with hitscanning, you don't need to actually cast the full ray out, just travel down the lookat axis and in order of objects closest to you, check their center position and size, and if they're within a particular margin THEN do the full check, otherwise skip over them

stackoverflow com/ questions / 41946007 / efficient-and-well-explained-implementation-of-a-quadtree-for-2d-collision-det

this was the first thing off google and the answers look pretty thorough

Implement collision on your "map", not objects. This way your map contains the objects that can intersect and it can automatically detect this when there is a collision. No need for constant scans.

Ehm... The map is just a tilebased projection of R^2.

stackoverflow.com/questions/41946007/efficient-and-well-explained-implementation-of-a-quadtree-for-2d-collision-det
What's with this fucking meme of obfuscating links? I see it everywhere nowadays. It this because some normie shit site bans all URLs?

You have a coordinate system right? That's a map. Whether it's 2d or 3d doesn't matter. When you add or move objects on the map you keep track of them and check for collission since you know which coordinates are taken.

How does that happen precisely?

Just use box2d.

Box2D physics are shit for platformers and other games where realism doesn't matter but feeling does.

Here's what I came up with. It assumes the top left corner is (0, 0) and the bottom right corner is (screen width, screen height).

bool intersect(Rectangle &x, Rectangle &y)
{
int xleft = x.center - x.width / 2;
int xright = x.center + x.width / 2;
int xtop = x.center - x.height / 2;
int xbottom = x.center + x.height / 2;
int yleft = y.center - y.width / 2;
int yright = y.center + y.width / 2;
int ytop = y.center - y.height / 2;
int ybottom = y.center + y.height / 2;

return xleft = yleft || xtop = ytop;
}


There's probably mistakes in there but the basic idea is that you only have to check if the bottom edge of one is beyond the top edge of the other and vice versa, and with the same idea for left and right.

Let's pretend we have a tile-based chessboard game. How do you know a tile is taken? You do you compare positions and calculate an intersect or simply ask the board object if there's already a piece there?

>ITT OOP brainlets

I find the lack of auto disgusting here

I have a pool of objects and test every pair of active ones.
>Pool: 100 entities
>CPU can raycast through 50k objects in no time
Call it bloat, I call it easy-to-write.

It's actually harder to write than what I'm suggesting.