### Author Topic: Modifying velocity after collision!  (Read 1898 times)

#### zuperxtreme

• Contributor
• Posts: 254
• Karma: +0/-0
##### Modifying velocity after collision!
« on: Mon, Mar 15, 2010 »
Agh, this must be so stupid.

I simply need the velocity of my object to be inverted:
http://www.swfcabin.com/open/1268705731(click to shoot/reset).

This is what I'm trying to use:

Code: [Select]
`if (this.collide(ballMC)) { ballMC.velocity.y *= -1;}/*if(overlapsPoint((ballMC.x+ballMC.width/2),(ballMC.y+ballMC.height))){ ballMC.velocity.y *= -1;}*/`The commented out part works, but not in corners. I COULD just check against every corner, but there must be a way with "collide".

What am I doing wrong?
..."without order nothing exists, without chaos nothing evolves"...
Zoklet.net

#### zuperxtreme

• Contributor
• Posts: 254
• Karma: +0/-0
##### Re: Modifying velocity after collision!
« Reply #1 on: Tue, Mar 16, 2010 »
No one? Should I be overriding collide?
..."without order nothing exists, without chaos nothing evolves"...
Zoklet.net

#### Liaf

• Member
• Posts: 19
• Karma: +0/-0
• yay!
##### Re: Modifying velocity after collision!
« Reply #2 on: Tue, Mar 16, 2010 »
Try out the following:
Write it before the super.update() if it's currently under it.
Write it after the super.update() if it's currently before.

I'm not really sure which one it was, but I had the same problem and it was easy to solve like that.

• Active Member
• Posts: 122
• Karma: +0/-0
##### Re: Modifying velocity after collision!
« Reply #3 on: Tue, Mar 16, 2010 »
when collide() detects a collision it automatically computes a new velocity vector for each colliding object, then it calls the proper hitYY() (hitTop, hitBottom, hitLeft, hitRight) for each colliding object and passes the new velocity vector. The default hitYY() changes the velocity using the new velocity!.

You could override every hitYY() and inverse the velocity: inverse x in hitLeft() and hitRight(), and inverse y in hitTop() and hitBottom(). But don't call super.hitYY().

#### zuperxtreme

• Contributor
• Posts: 254
• Karma: +0/-0
##### Re: Modifying velocity after collision!
« Reply #4 on: Wed, Mar 17, 2010 »
...

Can you be a bit more specific? All this OOP/classes/overriding business is rather new to me.

Could you post a snippet?
..."without order nothing exists, without chaos nothing evolves"...
Zoklet.net

#### zuperxtreme

• Contributor
• Posts: 254
• Karma: +0/-0
##### Re: Modifying velocity after collision!
« Reply #5 on: Thu, Mar 18, 2010 »
le'bump? :/
..."without order nothing exists, without chaos nothing evolves"...
Zoklet.net

• Active Member
• Posts: 122
• Karma: +0/-0
##### Re: Modifying velocity after collision!
« Reply #6 on: Thu, Mar 18, 2010 »
ok, create a new class and call it Ball, this class extends FlxSprite and will override the hitXX() methods to invert the velocities:
Code: [Select]
`public class Ball extends FlxSprite{ public function BallMC(X:Number, Y:Number, SimpleGraphic:Class = null) {                super(X,Y,SimpleGraphic); } override public function hitBottom(Contact:FlxObject, Velocity:Number):void { velocity.y *= -1;                onFloor = true; // useful for games with gravity } override public function hitTop(Contact:FlxObject, Velocity:Number):void { velocity.y *= -1; } override public function hitLeft(Contact:FlxObject, Velocity:Number):void { velocity.x *= -1; } override public function hitRight(Contact:FlxObject, Velocity:Number):void { velocity.x *= -1; }}`
Because Ball extends a FlxSprite you can use it like a FlxSprite.
This should (hopefully) do what you want, you'll still need to call this.collide(ball) this way you make sure the hitXX() functions will be called if a collision occurs.

« Last Edit: Thu, Mar 18, 2010 by haden.dude »