I Needed very very basic collision detection

Hi guys . I developing a game engine from scratch but i dont know how to add collision detection i googled over 10 x times but every source just slowed development of my project. I searching very basic collision detection can be buggy no problem. I needed AABB collisions and explain for me how to use my player coordinate values are: tra_x ,tra_y and tra_z they are int.

So make compatible with my code. I know i needed give my code but its closed sourced but i can give some parts of code.
My game engine have really basic .obj loader and .bmp loader.

my currently collision system:
if (tra_x > 3) {

tra_x-=0.1f;

}

if (tra_x > -3) {

tra_x -= 0.1f;

}

if (tra_z > 3) {

tra_z -= 0.1f;

}

if (tra_z > 3) {

tra_z -= 0.1f;

}

So i glad for all helps thanks for all replies :) im waiting for answer.

I needed very basic code . Internet filled with garbage with not working collision detection codes. I cant program it beacuse my alegebra is low. So i needed help.

But my current collision system is really bad. Only works for rooms. I needed very basic collision algorithm uses glm (opengl mathematics)
Last edited on
Just from reading your first paragraph, the big red flag I see is that you're saying tra_x, tra_y, and tra_z are ints, but you are trying to subtract floating-point numbers from them (e.g. 0.1f).

Perhaps you want tra_x/y/z to be floats instead?

But more generally speaking:
Step 1 is detecting collision.
Step 2 is actually handling the collision (e.g. moving the player to a corrective spot).
Focus on step 1 (just detecting collision) before you focus on step 2 (mitigating the collision).
e.g. if player is within a rectangle, print "COLLIDING".
If player is not within a rectangle, print "NOT COLLIDING".

If you scroll to the bottom of this page, you can see a function for bounding box collision test.
https://github.com/SFML/SFML/wiki/Source:-Simple-Collision-Detection-for-SFML-2

Aside from that, 4 if-statements with magic numbers in them doesn't give us much information to work with.
Last edited on
the fastest and most basic is just a radius.
grab the center of your object (this can be a part of a larger object, like the head of a person, or torso, whatever) and figure out roughly what radius sphere (or circle for 2-d) would go around the object. Just check the two radii. If the 2 spheres intersect, you have a collision.

This causes ugliness in graphics rich games with high resolution. You can see things bounce off each other without actually touching, for example, so its a little cheesy and old school. But it will serve as a placeholder until you find you want to do something more complicated.

you can usually avoid the square root for this, and just compare the squared values, though today the speedup is probably not a big deal unless you have a LOT of things hitting each other.

(if you are not great at math, what I am saying is that you take the center of object 1 and the center of object2, compute the distance of that straight line, and see if that is small enough to incur a hit (which is simply 'is it more than the sum of the 2 bounding sphere radii' which should be a compile time constant ... but you can square both sides to save a math step .. x*x+y*y+z*z <= (r*r+r2*r2) where the RHS is the constant).

note the a bounding box is the exact same thing, except instead of 1 radius, you have a distance for each dimension. So its more than 3 times the work, but significantly better looking result if you have many objects that are not approximately spherical. You have to track which dimensions are interacting (a minor pain if the objects are rotating as they move, etc).

It seems like any good graphics library would do this for you with a better approach.

if your math is pre algebra, use the simple sphere idea for now if your tools can't do it for you. You don't need algebra for what I said: do you know the basic geometric distance formula? sqrt(x*x+y*y+z*z)? Its just this idea (that is the distance from zero simple form). 2 points in space becomes sqrt( (x2-x1)^2 + (y2-y1)^2+ (z2-z1)^2) where ^2 means squared.
Last edited on
good explenation thanks i trying to write basic raycasting if player is near x,y,z values are eruded

thanks for formula but if someone give code is more better my code messed up i wroting basic raytracing but its now 148 lines of junk code
i still needed very basic code i tried it but now player cannot move anymore
1
2
3
4
5
6
bool impact( object a, object b)
{
   return ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z)) <= (a.radius+b.radius)*(a.radius+b.radius);
}



bool impact( object a, object b)
{
return ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z)) <= (a.radius+b.radius)*(a.radius+b.radius);
}

theres no struct about object a object b
theres no struct about object a object b

Nobody will write your program for you unless you hire them. Jonnin's code presents an idea. It's your job to adapt the explanation to your project.
theres no struct about object a object b


i added struct objects thanks now code working also i changed bit to make collisions realistic
so problem solved thanks....
Topic archived. No new replies allowed.