I'm trying to make a game like cut the rope and meet some problems.
when I create a float bubble and connect with a rope(a sets of bodies connected with revoluteJoint), the rope will become longer and when I destroy the bubble,rope gives a a big reaction force to candy, so how to decrease this reaction force?
to simulate bubble's floating, I did this
class Bubble extends PhyObject
{
override function draw():void{
if(this.combined){
this.getBody().SetLinearVelocity(velocity);//velocity=new b2Vec2(0,-3.5);
}
}
//other code
}
class Main{
public function loop(){
getAllPhyObject().forEach(function(phyObj){
phyobj.draw();
});
}
}
so if bubble was combined with candy ,It "float".
I think maybe I can do this
pseudo code:
if(bubble is connected with rope)
{
//dynamic adjust bubble's linear velocity with rope's reaction force
}
but I just cannot figure out how to dynamic adjust bubble's linear velocity.and is there a simpler way to solve this problem?
thanks!
update
finally I solved my problem,so I write it down and hope it help to you.
below code is grab from b2BuoyancyController and it works in my case.
//buoyancy
private function float():void {
if (bodies.length < 1)
return;
var body:b2Body = bodies[0];
if(body.IsAwake() == false){
//Buoyancy force is just a function of position,
//so unlike most forces, it is safe to ignore sleeping bodes
return;
}
var normal:b2Vec2 = new b2Vec2(0, -1);
var offset:Number = 20;
var density:Number = 2;
var velocity:b2Vec2 = new b2Vec2(0, 0);
var linearDrag:Number = 2;
var angularDrag:Number = 1;
var gravity:b2Vec2 = toolkit.world.GetGravity().Copy();
var areac:b2Vec2 = new b2Vec2();
var massc:b2Vec2 = new b2Vec2();
var area:Number = 0.0;
var mass:Number = 0.0;
for(var fixture:b2Fixture=body.GetFixtureList();fixture;fixture=fixture.GetNext()){
var sc:b2Vec2 = new b2Vec2();
var sarea:Number = fixture.GetShape().ComputeSubmergedArea(normal, offset, body.GetTransform(), sc);
area += sarea;
areac.x += sarea * sc.x;
areac.y += sarea * sc.y;
var shapeDensity:Number = 1;
mass += sarea*shapeDensity;
massc.x += sarea * sc.x * shapeDensity;
massc.y += sarea * sc.y * shapeDensity;
}
areac.x/=area;
areac.y/=area;
massc.x/=mass;
massc.y/=mass;
if(area<Number.MIN_VALUE)
return;
//Buoyancy
var buoyancyForce:b2Vec2 = gravity.GetNegative();
buoyancyForce.Multiply(density*area)
body.ApplyForce(buoyancyForce,massc);
//Linear drag
var dragForce:b2Vec2 = body.GetLinearVelocityFromWorldPoint(areac);
dragForce.Subtract(velocity);
dragForce.Multiply(-linearDrag*area);
body.ApplyForce(dragForce, areac);
//tmpForce
if(candy.bodies.length>0){ //TODO 你妹!!!!!
var tmpForce:b2Vec2 = candy.bodies[0].GetLinearVelocity();
body.SetLinearVelocity(tmpForce);
//tmpForce.x = 0;
//tmpForce.Subtract(velocity);
//tmpForce.Multiply(-linearDrag*area);
body.ApplyForce(tmpForce, areac);
}
//Angular drag
//TODO: Something that makes more physical sense?
body.ApplyTorque(-body.GetInertia()/body.GetMass()*area*body.GetAngularVelocity()*angularDrag)
}
tips:use force instated of impulse and changing linear velocity.