-
Notifications
You must be signed in to change notification settings - Fork 3
/
B2FlxTileblock.as
71 lines (57 loc) · 1.72 KB
/
B2FlxTileblock.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package
{
import org.flixel.*;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class B2FlxTileblock extends FlxTileblock
{
private var ratio:Number = 30;
//public var _def:b2PolygonDef;
public var _fixDef:b2FixtureDef;
public var _bodyDef:b2BodyDef
public var _obj:b2Body;
private var _width:Number;
private var _height:Number;
private var _world:b2World;
//Physics params default value
public var _friction:Number = 0.8;
public var _restitution:Number = 0.3;
public var _density:Number = 0.7;
//Default angle
public var _angle:Number = 0;
//Default body type
public var _type:uint = b2Body.b2_staticBody;
public function B2FlxTileblock(X:Number, Y:Number, Width:Number, Height:Number, w:b2World):void
{
super(X,Y, Width, Height);
_width = Width;
_height = Height;
_world = w;
}
override public function update():void
{
x = (_obj.GetPosition().x * ratio) - width/2 ;
y = (_obj.GetPosition().y * ratio) - height/2;
angle = _obj.GetAngle() * (180 / Math.PI);
super.update();
}
public function createBody():void
{
var boxShape:b2PolygonShape = new b2PolygonShape();
boxShape.SetAsBox((_width/2) / ratio, (_height/2) /ratio);
_fixDef = new b2FixtureDef();
_fixDef.density = _density;
_fixDef.restitution = _restitution;
_fixDef.friction = _friction;
_fixDef.shape = boxShape;
_bodyDef = new b2BodyDef();
_bodyDef.position.Set((x + (_width/2)) / ratio, (y + (_height/2)) / ratio);
_bodyDef.angle = _angle * (Math.PI / 180);
_bodyDef.type = _type;
_obj = _world.CreateBody(_bodyDef);
_obj.CreateFixture(_fixDef);
}
}
}