Instantly stop an object that is moving and collecting it

47 views
Skip to first unread message

Matthew Brandenburg

unread,
Apr 22, 2015, 1:07:05 PM4/22/15
to haxef...@googlegroups.com
I have a feeling that this might be easier than I am thinking it is, but figured I'd ask and see if someone could help me.  I am trying to stop a ball from moving when it hits a wall. However, I want the wall to be the sides of the screen.  What would be the easiest way to do this?  Is it just a collision or is it me changing the velocity to 0?  should I create a 1 px black rectangle that goes around the screen and test the collision against that?

And as a bonus question, any advice on keeping the ball on the wall and creating a pile of balls that stack up on the wall?

Sebastian Fernandez

unread,
Apr 23, 2015, 8:35:21 AM4/23/15
to haxef...@googlegroups.com
you can create the walls by making a FlxSprite, immovable=true and then test FlxG.collide(ball,wall).
if you set your walls on both sides of the screen make the worldBounds a little wider, to be sure that collisions against the wall works fine.

Other option is to test FlxG.overlap(ball,wall,myCollideFunction) and then create "myCollideFunction" where you set the ball's velocity to 0, and any other thing you wanna do.

FlxG.collide is simpler

Karl Penzhorn

unread,
May 1, 2015, 4:15:30 AM5/1/15
to haxef...@googlegroups.com
I've been struggling to understand movement and collision in flixel as well.

I still haven't found a good explanation of how movement is handled but from what I can tell it uses position, velocity and acceleration, and then updates them in the update() method ... which is where you need to come in to change things ...

So for example, in the dungeon crawler tutorial to make the keyboard move the character you use the keys to determine the character's velocity (not position) ...

I recently did my own collision and it was tricky - Once you detect a collision (for whatever reason) you have to move the character out of the collision zone, otherwise they get stuck !

What I ended up doing was putting my own collide() method onto my character:

public function collide(player:Player):Bool
{
var collided = FlxG.pixelPerfectOverlap(player,this);

if (collided)
{
player.x = player.x - 5*(player.x-player.last.x);
player.y = player.y - 5*(player.y-player.last.y);
player.velocity.x = 0;
player.velocity.y = 0;
}

return collided;
}

So this method is in the immovable object and I pass in the moving Player object (both are just sprites).

As you can see, when I detect a collision (here using the pixel perfect function - you can just use a global x or y) I actually move the character back based on their last position ('last' is set to the position of an object during the last update...). And in fact, I multiply that amount by 5 - Don't ask me why 5 ...

AND I set the player velocity to zero.

And amazingly it works... Though it was a nightmare trying to get it working using the standard collide() or overlap() stuff ...

Hope that helps

Eric Grice

unread,
May 1, 2015, 7:34:33 AM5/1/15
to haxef...@googlegroups.com

Previous two posters are right, but there's an easier way to do this.  In the update method where you're moving the ball, you can add a conditional to check the position of the ball.  What you want to do is check it against (I think) FlxG.width and FlxG.height.  Those two values are gonna be your bottom and right sides of the screen, and 0, 0 are gonna be the top and left sides of the screen.  So you can check the ball's position (minus or plus half the ball's width to check the edge of it instead of the center, if you're so inclined) against those values, depending on which direction it's traveling, and if it exceeds the value then set its velocity to zero.

It might be best to check what the next position of the ball is gonna be before you move it and, if it's less than 0 or greater than height/width, then set it to that value instead.

On Apr 22, 2015 1:07 PM, "Matthew Brandenburg" <brand...@gmail.com> wrote:
I have a feeling that this might be easier than I am thinking it is, but figured I'd ask and see if someone could help me.  I am trying to stop a ball from moving when it hits a wall. However, I want the wall to be the sides of the screen.  What would be the easiest way to do this?  Is it just a collision or is it me changing the velocity to 0?  should I create a 1 px black rectangle that goes around the screen and test the collision against that?

And as a bonus question, any advice on keeping the ball on the wall and creating a pile of balls that stack up on the wall?

--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to the Google Groups "HaxeFlixel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haxeflixel+...@googlegroups.com.
Visit this group at http://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/b44fbf3e-7217-4233-976d-3f20b49aab5f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages