Skip to main content
added 700 characters in body
Source Link
Luca
  • 101
  • 2

I'm thinking about how I should manage collisions in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is this:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling:

  1. A.collideWith(B)
  2. B.collidesWith(A)

B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be any collision anymore.

So, is it a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. In that way all should work fine.

But it is a good software design?

EDIT

I see, yes of course i check only for half. So there should be a class that checks for collisions, and should be like:

checkedpairs.clear();
for( Object a : objects )
{
    for( Object b : objects )
    {
        if( ! a.collideWith( b ) || checkedpairs.contains( a, b ) ) return;

        Object a1 = copyf( a );
        Object b1 = copyOf( b );

        a.collideWith( b1 );
        b.collideWith( a1 );

        checkedpairs.add( a, b );
    }
}

Instead of putting the logic for the swap in the objects themselves...

But that way, I'm creating two objects for each collision, isn't too much overhead?

The problem is that I don't need only the position, but al lot of physics stuff like velocity, acceleration, mass... Should I pass them? Isn't it too hard-coded?

I'm thinking about how I should manage collisions in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is this:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling:

  1. A.collideWith(B)
  2. B.collidesWith(A)

B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be any collision anymore.

So, is it a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. In that way all should work fine.

But it is a good software design?

I'm thinking about how I should manage collisions in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is this:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling:

  1. A.collideWith(B)
  2. B.collidesWith(A)

B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be any collision anymore.

So, is it a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. In that way all should work fine.

But it is a good software design?

EDIT

I see, yes of course i check only for half. So there should be a class that checks for collisions, and should be like:

checkedpairs.clear();
for( Object a : objects )
{
    for( Object b : objects )
    {
        if( ! a.collideWith( b ) || checkedpairs.contains( a, b ) ) return;

        Object a1 = copyf( a );
        Object b1 = copyOf( b );

        a.collideWith( b1 );
        b.collideWith( a1 );

        checkedpairs.add( a, b );
    }
}

Instead of putting the logic for the swap in the objects themselves...

But that way, I'm creating two objects for each collision, isn't too much overhead?

The problem is that I don't need only the position, but al lot of physics stuff like velocity, acceleration, mass... Should I pass them? Isn't it too hard-coded?

I'm thinking atabout how iI should manage collisionecollisions in my game. I'm I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is the sequentthis:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling: A.collideWith(B),

  1. A.collideWith(B)
  2. B.collidesWith(A)

B.collides With(A) B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be any collision anymore.

So, It is it a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. It In that way all should workswork fine. But

But it is a good software design?

Sorry for my english, Thanks to all!

I'm thinking at how i should manage collisione in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is the sequent:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling: A.collideWith(B), B.collides With(A) B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be collision anymore.

So, It is a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. It that way all should works fine. But it is a good software design?

Sorry for my english, Thanks to all!

I'm thinking about how I should manage collisions in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is this:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling:

  1. A.collideWith(B)
  2. B.collidesWith(A)

B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be any collision anymore.

So, is it a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. In that way all should work fine.

But it is a good software design?

Source Link
Luca
  • 101
  • 2

Double buffer - Managing Collision

I'm thinking at how i should manage collisione in my game. I'm thinking about having a "Collision" class that checks for collision, and in case takes actions to resolve them.

My problem is the sequent:

A and B moving to the other, when they collide they must bounce.

If I resolve the collisions sequentially, like calling: A.collideWith(B), B.collides With(A) B will be stuck, because A has already updated his velocity and position, and when will be called B.collideWith(A) there won't be collision anymore.

So, It is a good idea to use a double buffer pattern?

I'll write the values obtained by the collision resolver in some variables, they will be swapped to the real ones when all the collision has been resolved. It that way all should works fine. But it is a good software design?

Sorry for my english, Thanks to all!