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)
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".
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.