Skip to content

Commit

Permalink
fixed up the way mass and elasticity are handled by FlxObject.separate()
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamAtomic committed Apr 25, 2011
1 parent 4c2a6ae commit d1a8616
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
3 changes: 2 additions & 1 deletion org/flixel/FlxButton.as
Expand Up @@ -221,7 +221,8 @@ package org.flixel
override public function draw():void
{
super.draw();
label.draw();
if(label != null)
label.draw();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions org/flixel/FlxGroup.as
Expand Up @@ -254,7 +254,7 @@ package org.flixel
}
else
{
basic = getFirstAvail(ObjectClass);
basic = getFirstAvailable(ObjectClass);
if(basic != null)
return basic;
if(ObjectClass == null)
Expand Down Expand Up @@ -376,7 +376,7 @@ package org.flixel
*
* @return A <code>FlxBasic</code> currently flagged as not existing.
*/
public function getFirstAvail(ObjectClass:Class=null):FlxBasic
public function getFirstAvailable(ObjectClass:Class=null):FlxBasic
{
var basic:FlxBasic;
var i:uint = 0;
Expand Down
58 changes: 44 additions & 14 deletions org/flixel/FlxObject.as
Expand Up @@ -855,7 +855,7 @@ package org.flixel
*/
public function justTouched(Direction:uint):Boolean
{
return ((touching & Direction) && (wasTouching & Direction)) > NONE;
return ((touching & Direction) > NONE) && ((wasTouching & Direction) <= NONE);
}

/**
Expand Down Expand Up @@ -952,18 +952,32 @@ package org.flixel
//Then adjust their positions and velocities accordingly (if there was any overlap)
if(overlap != 0)
{
var obj1v:Number = Object1.velocity.x;
var obj2v:Number = Object2.velocity.x;

if(!obj1immovable && !obj2immovable)
{
overlap *= 0.5;
var object1velocityX:Number = Object1.velocity.x;
if(!obj1immovable)
Object1.x = Object1.x - overlap;
Object2.x += overlap;

var obj1velocity:Number = Math.sqrt((obj2v * obj2v * Object2.mass)/Object1.mass) * ((obj2v > 0)?1:-1);
var obj2velocity:Number = Math.sqrt((obj1v * obj1v * Object1.mass)/Object2.mass) * ((obj1v > 0)?1:-1);
var average:Number = (obj1velocity + obj2velocity)*0.5;
obj1velocity -= average;
obj2velocity -= average;
Object1.velocity.x = average + obj1velocity * Object1.elasticity;
Object2.velocity.x = average + obj2velocity * Object2.elasticity;
}
else if(!obj1immovable)
{
Object1.x -= overlap;
Object1.velocity.x = (Object2.mass/Object1.mass)*Object2.velocity.x - Object1.velocity.x*Object1.elasticity;
Object1.x = Object1.x - overlap;
Object1.velocity.x = obj2v - obj1v*Object1.elasticity;
}
if(!obj2immovable)
else if(!obj2immovable)
{
Object2.x += overlap;
Object2.velocity.x = (Object1.mass/Object2.mass)*object1velocityX - Object2.velocity.x*Object2.elasticity;
Object2.velocity.x = obj1v - obj2v*Object2.elasticity;
}
return true;
}
Expand Down Expand Up @@ -1037,21 +1051,37 @@ package org.flixel
//Then adjust their positions and velocities accordingly (if there was any overlap)
if(overlap != 0)
{
var obj1v:Number = Object1.velocity.y;
var obj2v:Number = Object2.velocity.y;

if(!obj1immovable && !obj2immovable)
{
overlap *= 0.5;
var object1velocityY:Number = Object1.velocity.y;
if(!obj1immovable)
Object1.y = Object1.y - overlap;
Object2.y += overlap;

var obj1velocity:Number = Math.sqrt((obj2v * obj2v * Object2.mass)/Object1.mass) * ((obj2v > 0)?1:-1);
var obj2velocity:Number = Math.sqrt((obj1v * obj1v * Object1.mass)/Object2.mass) * ((obj1v > 0)?1:-1);
var average:Number = (obj1velocity + obj2velocity)*0.5;
obj1velocity -= average;
obj2velocity -= average;
Object1.velocity.y = average + obj1velocity * Object1.elasticity;
Object2.velocity.y = average + obj2velocity * Object2.elasticity;
}
else if(!obj1immovable)
{
Object1.y = Object1.y - overlap;
Object1.velocity.y = (Object2.mass/Object1.mass)*Object2.velocity.y - Object1.velocity.y*Object1.elasticity;
if(Object2.immovable && Object2.moves && (obj1delta > obj2delta))
Object1.velocity.y = obj2v - obj1v*Object1.elasticity;
//This is special case code that handles cases like horizontal moving platforms you can ride
if(Object2.active && Object2.moves && (obj1delta > obj2delta))
Object1.x += Object2.x - Object2.last.x;
}
if(!obj2immovable)
else if(!obj2immovable)
{
Object2.y += overlap;
Object2.velocity.y = (Object1.mass/Object2.mass)*object1velocityY - Object2.velocity.y*Object2.elasticity;
if(Object1.immovable && Object1.moves && (obj1delta < obj2delta))
Object2.velocity.y = obj1v - obj2v*Object2.elasticity;
//This is special case code that handles cases like horizontal moving platforms you can ride
if(Object1.active && Object1.moves && (obj1delta < obj2delta))
Object2.x += Object1.x - Object1.last.x;
}
return true;
Expand Down
8 changes: 7 additions & 1 deletion org/flixel/FlxSprite.as
Expand Up @@ -355,6 +355,12 @@ package org.flixel
}
frameWidth = frameHeight = width = height = max;
resetHelpers();
if(AutoBuffer)
{
width = brush.width;
height = brush.height;
centerOffsets();
}
return this;
}

Expand Down Expand Up @@ -464,7 +470,7 @@ package org.flixel

/**
* This function draws or stamps one <code>FlxSprite</code> onto another.
* This function is NOT intended to replace <code>render()</code>!
* This function is NOT intended to replace <code>draw()</code>!
*
* @param Brush The image you want to use as a brush or stamp or pen or whatever.
* @param X The X coordinate of the brush's top left corner on this sprite.
Expand Down
2 changes: 1 addition & 1 deletion org/flixel/FlxText.as
Expand Up @@ -211,7 +211,7 @@ package org.flixel
}

/**
* The alignment of the font ("left", "right", or "center").
* The color of the text shadow in 0xAARRGGBB hex format.
*/
public function get shadow():uint
{
Expand Down
4 changes: 3 additions & 1 deletion org/flixel/system/debug/Perf.as
Expand Up @@ -96,10 +96,12 @@ package org.flixel.system.debug
{
var time:int = getTimer();
var elapsed:int = time - _lastTime;
var updateEvery:uint = 500;
if(elapsed > updateEvery)
elapsed = updateEvery;
_lastTime = time;

_updateTimer += elapsed;
var updateEvery:uint = 500;
if(_updateTimer > updateEvery)
{
var i:uint;
Expand Down

0 comments on commit d1a8616

Please sign in to comment.