Hey there,
I've been working on a game that uses explosions/impulses in quickbox2d (you don't have to know how to use quickbox2d, i can read box2d code..) ... (code is below)
The code i'm working with now isn't bad and all, however it's not good
enough. Currently, when you click, it applys a force to objects that are
only within a certain radius of the click. As long as the center of the
object is within the radius, it works.
However, if I get an object that is really long and click at the end of it,
it doesn't do anything because the center point is not within the radius.
What i want to do is be able to apply an impulse to all the boxes in that
radius regardless of if the center is within the radius(the end of the box
is in the radius, the click will not affect boxes outside the radius). I
was thinking about how to do it, and the only why i could come up with shoot
some little circles every 22.5 degrees (so, 16 outwards from the click
equidistantly from eachother). I haven't tried it, and i don't want to, it
sounds really inefficient.
Can you help me out with this?
heres the code i use:
Also, would my impulse code work for group objects counting them as one
solid object, or would it apply impulses to each component of the group
object?
Also, i can explain further what i'm looking to do if you need.
Thanks.
I've been working on a game that uses explosions/impulses in quickbox2d (you don't have to know how to use quickbox2d, i can read box2d code..) ... (code is below)
The code i'm working with now isn't bad and all, however it's not good
enough. Currently, when you click, it applys a force to objects that are
only within a certain radius of the click. As long as the center of the
object is within the radius, it works.
However, if I get an object that is really long and click at the end of it,
it doesn't do anything because the center point is not within the radius.
What i want to do is be able to apply an impulse to all the boxes in that
radius regardless of if the center is within the radius(the end of the box
is in the radius, the click will not affect boxes outside the radius). I
was thinking about how to do it, and the only why i could come up with shoot
some little circles every 22.5 degrees (so, 16 outwards from the click
equidistantly from eachother). I haven't tried it, and i don't want to, it
sounds really inefficient.
Can you help me out with this?
heres the code i use:
Code:
function doExplosion(pos:b2Vec2):void {
var Body:b2Body = null;
var HitVector:b2Vec2 = new b2Vec2(0,0);
var BodyPos:b2Vec2 = new b2Vec2(0,0);
var HitForce:Number = 0;
var Distance:Number = 0;
for (var i:int = 0; i < boxes.length; ++i) { // holds references to all objects that are not 0 density
Body = boxes[i].body;
Body.WakeUp();
BodyPos = Body.GetWorldCenter();
HitVector.x = BodyPos.x - pos.x;
HitVector.y = BodyPos.y - pos.y;
Distance = HitVector.Normalize();
if ((Body.IsDynamic()) && (Distance<=Radius)) {
HitForce=(Radius-Distance)*Force;
HitVector.x *= HitForce;
HitVector.y *= HitForce;
Body.ApplyImpulse(HitVector, Body.GetWorldCenter());
}
}
}
var Body:b2Body = null;
var HitVector:b2Vec2 = new b2Vec2(0,0);
var BodyPos:b2Vec2 = new b2Vec2(0,0);
var HitForce:Number = 0;
var Distance:Number = 0;
for (var i:int = 0; i < boxes.length; ++i) { // holds references to all objects that are not 0 density
Body = boxes[i].body;
Body.WakeUp();
BodyPos = Body.GetWorldCenter();
HitVector.x = BodyPos.x - pos.x;
HitVector.y = BodyPos.y - pos.y;
Distance = HitVector.Normalize();
if ((Body.IsDynamic()) && (Distance<=Radius)) {
HitForce=(Radius-Distance)*Force;
HitVector.x *= HitForce;
HitVector.y *= HitForce;
Body.ApplyImpulse(HitVector, Body.GetWorldCenter());
}
}
}
Also, would my impulse code work for group objects counting them as one
solid object, or would it apply impulses to each component of the group
object?
Also, i can explain further what i'm looking to do if you need.
Thanks.