Author Topic: [SOLVED] Easiest way to bounce Sprite objects from walls.  (Read 2023 times)

notsuresure

  • Member
  • **
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Hi. I'm new to coding. Very new. I have been experimenting with movement and I'm starting to experiment with interactions between objects. I managed to make a simple application in Python where objects bounce off walls. I'm now trying this in AS3 using Flixel, and I'm having problems.

So far I think I have to use FlxG.collide(Obj1, Obj2, function) to detect collision, but I can't figure out how to implement the function.

What I did in Python was: check for collision on top or bottom walls,  if true reverse y velocity; check for collision in left and right walls, if true reverse x velocity. But the FlxG.collide() method returns a boolean! I know the objects collided, but I don't where, and which parts of the objects.

I'm very lost here. How would you implement call collision and bounce?

Thanks for your time.
« Last Edit: Mon, Nov 26, 2012 by notsuresure »

mightiest_hero

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Easiest way to bounce Sprite objects from walls.
« Reply #1 on: Sun, Nov 25, 2012 »
you can use FlxG.overlap(Obj1, Obj2, function) to check collision

then set movement on function

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile
Re: Easiest way to bounce Sprite objects from walls.
« Reply #2 on: Sun, Nov 25, 2012 »
In Flixel 2.55, FlxObject has an elasticity variable you can use for this purpose.  (FlxSprite derives from FlxObject.)

Code: [Select]
elasticity = 1;
When you call FlxG.collide(), it checks if the objects overlap, and if they do, it calls FlxObject.separate() to move them apart.  When it does this, it also adjusts their velocities.  If elasticity is 0, it stops the objects cold.
« Last Edit: Sun, Nov 25, 2012 by auriplane »

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: Easiest way to bounce Sprite objects from walls.
« Reply #3 on: Mon, Nov 26, 2012 »
auriaplane is right, that should work really well. Also, to prevent the wall from moving set:

Code: [Select]
wall.immovable = true;
Obviously, only up to one of two colliding objects can be immoveable.

tincho_kudos

  • Member
  • **
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Based on my experience if two objects (one immovable, and the other not immovable) collides, the second object (the immovable =false) slows downs in terms of velocity
Is there a way to prevent this?
« Last Edit: Thu, Nov 29, 2012 by tincho_kudos »

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Based on my experience if two objects (one immovable, and the other not immovable) collides, the second object (the immovable =false) slows downs in terms of velocity
Is there a way to prevent this?

It shouldn't if that object's elasticity is 1. Velocity will be inversed though.