Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075922 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:45:45 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Physics system problem
Pages: [1]
Print
Author Topic: Physics system problem  (Read 1081 times)
theman515
Level 0
**


The code... the code!!


View Profile WWW Email
« on: July 21, 2011, 04:55:52 PM »

I'm rolling my own physics system for my game and I piled all of my game objects into a list.

I'm using the boost::shared_ptr for each object and use a BOX struct for my bounding box, the getLeft() and getRight() etc, is to get the x co-ordinate and the x co-ordinate+ the width.

to move the characters i use a force vector and edit it using the setForceX and setForceY etc

here's my code:

Code:
void P_System::update()
{
    Iterator bumper;
    for(m_lListIt = m_lList->begin(); m_lListIt != m_lList->end(); m_lListIt++)
    {
        for(bumper = m_lList->begin();bumper!=m_lList->end();bumper++)
        {
            //if it hits the top or bottom
            if((m_lListIt->get()->getLeft() <= bumper->get()->getRight() && m_lListIt->get()->getRight() >= bumper->get()->getLeft()) && (m_lListIt->get()->getDown() <= bumper->get()->getUp() || m_lListIt->get()->getUp() >= bumper->get()->getDown()))
            {
                cout << 1;
                m_lListIt->get()->setForceY(0);
                bumper->get()->setForceY(0);
            }
            if((m_lListIt->get()->getLeft() <= bumper->get()->getRight() || m_lListIt->get()->getRight() >= bumper->get()->getLeft()) && (m_lListIt->get()->getDown() <= bumper->get()->getUp() && m_lListIt->get()->getUp() >= bumper->get()->getDown()))
            {
                m_lListIt->get()->setForceX(0);
                bumper->get()->setForceX(0);
            }

        }
    }
    for(m_lListIt = m_lList->begin(); m_lListIt != m_lList->end(); m_lListIt++)
    {
        m_lListIt->get()->setPosition(m_lListIt->get()->getX()+m_lListIt->get()->getForceX(),m_lListIt->get()->getY()+m_lListIt->get()->getForceY());
    }
}

what am i doing wrong? thanks in advance  Noir
Logged
SFBTom
Level 1
*



View Profile WWW
« Reply #1 on: July 22, 2011, 03:30:46 AM »

What's going wrong with the code? What are the symptoms?
Logged

bateleur
Level 10
*****



View Profile
« Reply #2 on: July 22, 2011, 03:49:41 AM »

I find this style fairly unreadable, but as far as I can see this:

Code:
if((m_lListIt->get()->getLeft() <= bumper->get()->getRight() && m_lListIt->get()->getRight() >= bumper->get()->getLeft()) && (m_lListIt->get()->getDown() <= bumper->get()->getUp() || m_lListIt->get()->getUp() >= bumper->get()->getDown()))

...says this (in pseudocode)...

Code:
if (m.left() < b.right && m.right > b.left && (m.down() < b.up() || m.up() > b.down))

...which checks that the character overlaps the buffer horizontally and then checks that either it's bottom edge is below the top of the buffer or its top edge is above the bottom of the buffer. However, that might not be a collision at all.

Consider, for example, the case where the character is a long way directly above the buffer. His top edge is way above the bottom of the buffer, so the expression will evaluate to true. This doesn't seem to be what you want, because no collision has occurred.
Logged

sorceress
Level 5
*****


Location: England


View Profile
« Reply #3 on: July 22, 2011, 08:42:27 AM »

What can sometimes be helpful in logic is to look at the opposite problem: under what conditions will the character not collide with the object?

if the four corners of the character's bounding box are to the left of the object.
if the four corners of the character's bounding box are to the right of the object.
if the four corners of the character's bounding box are above the object.
if the four corners of the character's bounding box are below the object.
Logged

I ain't pushing no moon buttons.
rivon
Level 10
*****



View Profile
« Reply #4 on: July 22, 2011, 09:57:46 AM »

I would look at SDL_collide - just rewrite the int SDL_CollideBoundingBox(SDL_Rect a , SDL_Rect b) method.
Logged
st33d
Guest
« Reply #5 on: July 24, 2011, 01:29:12 PM »

What can sometimes be helpful in logic is to look at the opposite problem: under what conditions will the character not collide with the object?

if the four corners of the character's bounding box are to the left of the object.
if the four corners of the character's bounding box are to the right of the object.
if the four corners of the character's bounding box are above the object.
if the four corners of the character's bounding box are below the object.

But don't write it that way. Calculating the ! for a positive collision costs time in some languages.
Logged
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #6 on: July 24, 2011, 01:45:06 PM »

I find this style fairly unreadable

Second this. The denser the code the harder it is to spot errors. If for no other reason, add descriptive variables for the benefit of the readers you're asking for help. Doing that might also give away the error while you're doing it.
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic