Collide Vs. Displace Interactions

Assume 2 sprites, car and truck. Is there a difference between these two interections?

truck.displace.car

and

car.collide.truck

Just wondering.

Thanks,

John

Hi @jwilson25,

Have you seen the great example in the documentation for any of the 4 collision types in GameLab? It really does a good job of visualizing the 4 different collision types. You can find the example in the documentation for displace HERE.

Hope that helps:) I find it super helpful when kids need to decide which is best for their project.
~Michelle

Hello @jwilson25,

the biggest difference between the two is how they interact with sprite velocity
collide will stop all velocity
displace will not
overall displace allows for better verbose velocity control but if managed incorrectly it can cause clipping with velocity values exceeding the frame collision test, collide is overall the best choice for beginners so less unexpected behavior occurs @melynn has provided great example so follow up with the link if my explanation was not clear enough

Varrience

Thank you @melynn and @varrience for your replies.

I have seen the examples that @melynn mentioned and I understand each of them as demonstrated.

Here is a remix of that example with the bounce and bounceoff examples commented out.

  • I changed it so the left block displaces the right block on the top line and the right block collides with the left block on line 2.
  • I also assigned a random velocityX to the blocks on the right.

Note that each time you run this code, the displace and collide behave the same way when the target block is switched.

@varrience, can you give an example of when displace and collide do not behave the same way when the target block is switched? I am not sure what you mean by “verbose velocity control”, although I do believe I have seen what you might mean by “clipping” in the CSD Unit 3 Lesson 24 (Flyer Game). But I am not sure.

Thanks!

John

Seems the behavior changes if both sprites have velocity
collide seems to inherit the velocity of the object it is colliding with if it’s zero it will no longer move
displace will only stop both objects from moving past each other

An example like this should suffice

var rightVelocity = randomNumber(-3, 1);
var spriteDisplace1 = createSprite(75, 50, 50, 50);
spriteDisplace1.setAnimation("displace");
spriteDisplace1.scale = 0.75;
// spriteDisplace1.velocityX=2;
var spriteDisplace2 = createSprite(250, 50, 50, 50);
spriteDisplace2.setAnimation("displaceTarget");
spriteDisplace2.scale = 0.75;
spriteDisplace2.velocityX = rightVelocity;
var spriteCollide2 = createSprite(75, 150, 50, 50);
spriteCollide2.setAnimation("collideTarget");
spriteCollide2.scale = 0.75;
// spriteCollide2.velocityX=2;
var spriteCollide1 = createSprite(250, 150, 50, 50);
spriteCollide1.setAnimation("collide");
spriteCollide1.scale = 0.75;
spriteCollide1.velocityX = rightVelocity;

function draw() {
  background("white");
  spriteDisplace1.displace(spriteDisplace2);
  spriteCollide1.collide(spriteCollide2);
  drawSprites();
}
setTimeout(function(){
  spriteDisplace1.destroy();
  spriteCollide2.destroy();
}, 3e3)

this shows my change i was referring to it seems to be the same with the collision logic while in motion as well but it’s more so inheritance than it directly being set to zero (of which i was not sure of until further testing)

in which case by using displace you would have to be explicitly changing (I.E. verbose changes) velocities by testing if (sprite.displace(sprite2)) {} instead of it inheriting the object it is colliding with which can cause issues with gravity and platformers

1 Like

@varrience This is very helpful. Thank you. I see what you mean. setting spriteDisplace1.velocityX=-0.2 and spriteCollide2.velocityX=-0.2; and increasing the timeout value from 3e3 to 5e3 is also helpful in visualizing the differences.