flash - Box2DFlash ApplyForce() -
i developing new game using box2dflash. write simple code in when click in stage ball move. use applyforce() function accomplish this. problem when ball start move not stop until goes corner of stage. continuously move given velocity. there way decrease velocity of ball per time when moving? code follow -
package { import box2d.collision.shapes.b2circleshape; import box2d.collision.shapes.b2polygonshape; import box2d.common.math.b2vec2; import box2d.common.math.b2vec3; import box2d.dynamics.b2body; import box2d.dynamics.b2bodydef; import box2d.dynamics.b2debugdraw; import box2d.dynamics.b2fixturedef; import box2d.dynamics.b2world; import flash.display.movieclip; import flash.display.sprite; import flash.events.event; import flash.events.keyboardevent; import flash.events.mouseevent; public class main extends movieclip { private var world:b2world; private const scale:number = 30; private var b1:b2body; public function main() { if (stage) init(); else addeventlistener(event.added_to_stage, init); trace("physics1..."); } private function init(e:event=null):void { removeeventlistener(event.added_to_stage, init); getstarted(); } private function getstarted():void { createworld(); debugdraw(); drawbox(stage.stagewidth / 2, stage.stageheight - 5, stage.stagewidth, 10); drawbox(stage.stagewidth / 2, 5, stage.stagewidth, 10); drawbox(5, stage.stageheight / 2, 10, stage.stageheight); drawbox(stage.stagewidth - 5, stage.stageheight / 2, 10, stage.stageheight); b1 = drawball(10, stage.stagewidth / 2, stage.stageheight / 2); b1.getfixturelist().setrestitution(0.4); b1.getfixturelist().setfriction(1.0); b1.getfixturelist().setdensity(20); addeventlistener(event.enter_frame, update); stage.addeventlistener(mouseevent.click, onclick); } private function onclick(e:mouseevent):void { b1.applyforce(new b2vec2(200, 200), b1.getworldcenter()); } private function update(e:event):void { world.step(1 / 30, 10, 10); world.clearforces(); world.drawdebugdata(); } private function createworld():void { world = new b2world(new b2vec2(0, 0), true); } private function debugdraw():void { var dd:b2debugdraw = new b2debugdraw(); var sp:sprite = new sprite(); addchild(sp); dd.setsprite(sp); dd.setdrawscale(scale); dd.setflags(b2debugdraw.e_shapebit); world.setdebugdraw(dd); } private function drawball(r:number, _x:number, _y:number):b2body { var bd:b2bodydef = new b2bodydef(); bd.position.set(_x / scale, _y / scale); bd.type = b2body.b2_dynamicbody; var fd:b2fixturedef = new b2fixturedef(); var ps:b2polygonshape = new b2polygonshape(); ps.setasbox(r / scale, r / scale); fd.shape = new b2circleshape(r / scale); var b:b2body = world.createbody(bd); b.createfixture(fd); return b; } private function drawbox(_x:number,_y:number,_w:number,_h:number):b2body { var bd:b2bodydef = new b2bodydef(); bd.position.set(_x / scale, _y / scale); var ps:b2polygonshape = new b2polygonshape(); ps.setasbox(_w / 2 / scale, _h / 2 / scale); var fd:b2fixturedef = new b2fixturedef(); fd.shape = ps; var b:b2body = world.createbody(bd); b.createfixture(fd); return b; } } }
you can add linear damping body:
b1.setlineardamping(3);
Comments
Post a Comment