Graalians

Graalians (https://www.graalians.com/forums/index.php)
-   Off-Topic Chat (https://www.graalians.com/forums/forumdisplay.php?f=14)
-   -   Programming Hit Detection (https://www.graalians.com/forums/showthread.php?t=23621)

Skill 03-05-2014 02:09 PM

Programming Hit Detection
 
How would one go about doing this? I know how to calculate if 2 circles collide using distance, and boxes colliding using if the 2 items are within each other.

But how the heck would I code collision detection from scratch using items using a rotated box? Can someone explain in pseudo code or c++?

Johnaudi 03-05-2014 02:20 PM

Are you talking 3D or 2D?

Or just mathematically speaking?

hosler 03-05-2014 02:38 PM

If distx is 0 and disty is 0 then hit

Johnaudi 03-05-2014 02:48 PM

Here's a small C++ code I've forked off, it's just hit detection for circles, though you get the concept hopefully.

PHP Code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    
// x1 and y1 are the coordinated of the first circle, and r1 is it's ray. (2 * r1 is the diameter)
    
double x1y1r1x2y2r2_unusedvar;

    
cout << "Insert the coordinates and the ray of the first circle seperated by spaces. (x y ray)" << endl;

    
cin >> x1 >> y1 >> r1;

    
cout << "Insert the coordinates and the ray of the second circle seperated by spaces. (x y ray)" << endl;

    
cin >> x2 >> y2 >> r2;

    if (
sqrt(pow(x1 x22) + pow(y1 y22)) < r1 r2) { // The mathematical process, sqrt() means square root and pow(number, power) is the number to the power of
        
cout << "Yes, the circles do collide." << endl;
    } else 
cout << "These circles do not collide" << endl;

    
cin >> _unusedvar// Just so the program won't quit.

    
return 0;



Dusty 03-05-2014 05:05 PM

Are you using any libraries? Typically they will provide you with methods to detect collisions between shapes.

Skill 03-06-2014 10:22 PM

Quote:

Posted by Johnaudi (Post 457774)
Are you talking 3D or 2D?

Or just mathematically speaking?

2d.

Quote:

Posted by Dusty (Post 457800)
Are you using any libraries? Typically they will provide you with methods to detect collisions between shapes.

It's not a full fledged game engine, I'll likely need to code hit detection from scratch. But for reference, I'm using the dragonfiresdk libraries.

Quote:

Posted by hosler (Post 457776)
If distx is 0 and disty is 0 then hit

That only works for rectangular hotboxes. Doesn't work for a rotated plane.

I guess the problem I'm having is that i can't mathematically figure out if two objects collide. Let's say for example you have one rectangle that is constantly rotating, and your character using a circular(or square) hitbox). How would you detect collision between the two? The distance formula wouldn't work, as it is not only circles.

Dusty 03-06-2014 10:23 PM

Quote:

Posted by Skill (Post 458071)
It's not a game engine, I need to code hit detection from scratch.

A library isn't the same thing as a game engine.

NeoZX 03-06-2014 11:17 PM

As long as the hit box it good enough for the player to hit it without a trick shot, it's good.

Skill 03-06-2014 11:25 PM

Quote:

Posted by Dusty (Post 458072)
A library isn't the same thing as a game engine.

I know. DragonfireSDK is essentially a collection of libraries. But I don't think any of them include hit detection functions.

Dusty 03-06-2014 11:42 PM

Quote:

Posted by Skill (Post 458090)
I know. DragonfireSDK is essentially a collection of libraries. But I don't think any of them include hit detection functions.

Detecting irregular shape collisions might be difficult(hence why I suggested checking that your library hopefully does. It's a pity it doesn't).

I found this, if you're curious on reading:
http://seb.ly/2009/05/super-fast-tri...rsection-test/

There's a lot you can probably find on shape collision on google. The languages might be different but the logic is always the same.

I mean, it sounds like you're dealing with some complicated interactions(balls and rotating rectangles and such), if you're interested in doing even some basic physics stuff involving these objects interacting it might be beneficial looking into a library that will help you out.

Anyways, if you want to try to start working it out yourself, then imo the first thing I would do is work out whether a single point(your mouse) is within a shape.

After that, you just need to create bounding shapes out of a group of these points.

glue 03-25-2014 03:52 AM

I'm not sure but I think that this link and this link could help you.


All times are GMT. The time now is 03:04 PM.

Powered by vBulletin/Copyright ©2000 - 2025, vBulletin Solutions Inc.