Jump to content

  • Log In with Google      Sign In   
  • Create Account

Collision Detection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 17 March 2012 - 11:04 AM

Well, Iv'e been trying to implement collision detection to a ball and a plane surface, supposely a rectangle.
Theres a lot of rectangles, I store them in a vector.
By far, i havent been impressed by the collision detection, Any help if im doing it wrong?

//This is how i store a vector of rectangles;
static std::vector<std::vector<Bricks> > Level(sizeof(map)/sizeof(*map), std::vector<Bricks>(sizeof(map)/sizeof(*map)));

//Collision detection
bool Brick::Collide( float x, float y, float r )
{
    float X, Y;
    for( size_t xSize = 0; xSize < Level.size(); xSize++ )
    {
        for( size_t ySize = 0; ySize < Level[xSize].size(); ySize++ )
        {
            if(Level[xSize][ySize].alive)
            {
                if( x < Level[xSize][ySize].x )
                {
                    X = Level[xSize][ySize].x;
                }
                else if( x > Level[xSize][ySize].x + Level[xSize][ySize].w )
                {
                    X = Level[xSize][ySize].x + Level[xSize][ySize].w;
                }
                else
                {
                    X = x;
                }
                if( y < Level[xSize][ySize].y )
                {
                    Y = Level[xSize][ySize].y;
                }
                else if( y > Level[xSize][ySize].y + Level[xSize][ySize].h )
                {
                    Y = Level[xSize][ySize].y + Level[xSize][ySize].h;
                }
                else
                {
                    Y = y;
                }
                if( sqrt( pow((long double)X-x, 2)+pow((long double)Y-y, 2)) < r )
                {
                    return true;
                }
            }
        }
    }
    return false;
}


Can anyone help me out, To do a almost-perfect collision detection.
Also, How do i implement collision for the sides of the rectangles, Since the ball is a moving object, when the ball hits the sides, It goes crazy, like zigzag or something.
Thanks.

Sponsor:

#2 boogyman19946   Members   -  Reputation: 1079

Like
0Likes
Like

Posted 18 March 2012 - 09:48 AM

It doesn't sound like your problem is in the collision detection, more like in collision response.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS