Author Topic: Set "Allow collisions" only for an object?  (Read 1560 times)

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile
Set "Allow collisions" only for an object?
« on: Tue, Oct 2, 2012 »
Hello,
Is there an easy way to set allowcollisions only for an object?
Let me detail it:
I have an object :object1, and another object :object2.
Object1 must  collide with all sides (ANY) with all other objects in game.
Object2 must  collide with all sides (ANY) with all other objects in game.
Object1 must collide ONLY  UP and DOWN with object2
How can I achieve this?

LaughingLeader

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • LaughingLeader Blog
Re: Set "Allow collisions" only for an object?
« Reply #1 on: Wed, Oct 3, 2012 »
If I'm understanding you correctly, you want Object 1 and 2 to collide with all other objects in the game, but want them to only respond with specific logic between one another?

I would use the Object1.overlaps(Object2) boolean to then call separate between Object1 and Object2.

Meaning it could be something like this:

Code: [Select]
if(Object1.overlaps(Object2)==true)
{
   if(Object1.isTouching(FlxObject.UP || Object1.isTouching(FlxObject.DOWN)
   {
         FlxObject.separate(Object1, Object2);
   }
}

If the isTouching checks don't work, then you could always work in some variables in there to calculate where Object1 is in reference to Object2. For instance, you could compare Object1 and Object2's y values and then if Object1 is higher or lower than Object2, separate them.

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile
Re: Set "Allow collisions" only for an object?
« Reply #2 on: Wed, Oct 3, 2012 »
Well the overlap code doesn;t work...
Code: [Select]
if (player.y + player.height/1.4  < playerwasd.y) {FlxG.collide(player, playerwasd)}
else { };
if (playerwasd.y + playerwasd.height/1.4  < player.y) {FlxG.collide(player, playerwasd)}
else { };

and yes the check on y works but still not  perfect.
While a player is above another he can "push it " it if falls and right after overlaps into it.
I thought it was an easier, elegant solution , maybe  this is something for 3.0

LaughingLeader

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • LaughingLeader Blog
Re: Set "Allow collisions" only for an object?
« Reply #3 on: Wed, Oct 3, 2012 »
Is there any reason you're using FlxG.collide instead of just FlxObject.separate? Perhaps your objects are both groups?

FlxG.collide calls FlxG.overlap and then separates them, which makes your code there potentially messy since FlxG.overlap creates a FlxQuadTree and destroys it again every update loop.

If they're both just objects (or sprites or whatever) I would try and implement a player.overlaps(playerwasd) if/else statement like I suggested, since it's more memory-friendly and a bit easier to control.

What is the type of object you're trying to collide with? Perhaps some more information could help find you a solution quicker.

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile
Re: Set "Allow collisions" only for an object?
« Reply #4 on: Wed, Oct 3, 2012 »
My objects are 2 players. One controlled by arrow one by wasd.
So no groups.
I tried with separate same results. Only that collide has a callback function that may be useful .
My next problem is when a player stands on top of the other and the player below jumps the jump is very short. Obvious because is colliding with the player above.
I want the player from above to jump normal like here:http://www.arcadeprehacks.com/game/17085/Splitman.html
My jumping code:
Code: [Select]
if((_jump >= 0) && (FlxG.keys.justPressed("C"))) //You can also use space or any other key you want
            {
                climbing = false;
_jump += FlxG.elapsed;
                if(_jump > 0.25) _jump = -1; //You can't jump for more than 0.25 seconds
            }
            else _jump = -1;
 
            if (_jump > 0)
            {
                if(_jump < 0.035)   // this number is how long before a short slow jump shifts to a faster, high jump
                    velocity.y = -.8 * maxVelocity.y; //This is the minimum height of the jump

else
velocity.y = -.8 * maxVelocity.y;

EDIT: it seams that SEPARATEY does the trick for perfect allow collisions  only for an object:
Code: [Select]
if (player.y + player.height/1.4  < playerwasd.y) FlxObject.separateY(player, playerwasd)}
else { };
if (playerwasd.y + playerwasd.height/1.4  < player.y) {FlxObject.separateY(player, playerwasd)}
else { };

But doesn't have a callback function that may be usefull for the next step(jumping)..
« Last Edit: Wed, Oct 3, 2012 by paala »

LaughingLeader

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • LaughingLeader Blog
Re: Set "Allow collisions" only for an object?
« Reply #5 on: Wed, Oct 3, 2012 »
My objects are 2 players. One controlled by arrow one by wasd.
So no groups.
I tried with separate same results. Only that collide has a callback function that may be useful .
My next problem is when a player stands on top of the other and the player below jumps the jump is very short. Obvious because is colliding with the player above.
I want the player from above to jump normal like here:

Ah, okay. So, is the problem with the player on top or the player below? If it's the player below who has the jump problem, you could add in a boolean check before the separate code:
Code: [Select]
//This is in the overlaps check if statement:
if(!player1.jumping)
{
    FlxObject.separate(player1, player2);
}
// If you want the second player to move with the first when they jump:
else
{
  if(!player2.jumping)
   {
      player2.velocity.y = player1.velocity.y;(you may want to subtract some from the velocity here in case he flies too high).
   }
   FlxObject.separate(player1, player2);
}

You could refine this a bit by detecting if only the player on bottom is jumping or not. In your jump logic you'd need to set a jumping boolean to true of course, and then false when gravity takes in.

Though, that would make it so that when the player on bottom jumps, they go through the other player like they would if they ran through them (this would be if you didn't have the else statement I included).

If you want the player on top to stay on top when the other player jumps, you'll probably have to mess with the second player's velocity when the first player jumps (if they're overlapping and being separated).

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile
Re: Set "Allow collisions" only for an object?
« Reply #6 on: Wed, Oct 3, 2012 »
Moving two objects together is complicated.  If the bottom player jumps, it'll need to update the top player object position relative to its movement, so the top player continues to think it's standing still and is touching down, but can still move on its own.  You'll need to detect the "attached" condition and handle it specially.  Flixel won't do it for you.

(This is similar to the problem of moving platforms, which can be a little annoying to implement with Flixel.  You might be able to find a solution elsewhere in the forums.)

BTW, instead of separate, try separateY.  Or FlxG.overlap(object1, object2, yourCallback, FlxObject.separateY), or such, I guess.