我正在使用box2d。我有一个或多个椭圆在屏幕上弹跳,我希望在椭圆之间产生一个关节,当它们彼此相距一定距离时。我想问题是如何确定Box2d世界中一个身体的坐标位置
到目前为止,我有:
import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.dynamics.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;
// A reference to our box2d world
Box2DProcessing box2d;
Mover[] movers = new Mover[99];
void setup() {
size(1640,1360);
smooth();
box2d = new Box2DProcessing(this);
box2d.createWorld();
// No global gravity force
box2d.setGravity(0,0);
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(8,16),random(width),random(height),random(-100,100));
}
}
void draw() {
background(255);
box2d.step();
for (int i = 0; i < movers.length; i++) {
for (int s = 0; s < movers.length; s++) {
Vec2 force = movers[s].attract(movers[i]);
movers[i].applyForce(force);
*******************This is the part I can't figure out*****************
////if(distance between mover[i] and mover[s]<10){create a join as follows:
//DistanceJointDef djd = new DistanceJointDef();
//// Connection between previous particle and this one
//djd.bodyA = mover[s].body;
//djd.bodyB = mover[i].body;
//// Equilibrium length
//djd.length = box2d.scalarPixelsToWorld(len);
//// Make the joint.
//DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd);
//}
************************************************************************
}
movers[i].display();
}
}
class Mover {
Body body;
float r;
float G;
Mover(float r_, float x, float y,float G_) {
r = r_;
G=G_;
// Define a body
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
// Set its position
bd.position = box2d.coordPixelsToWorld(x,y);
body = box2d.world.createBody(bd);
// Make the body's shape a circle
CircleShape cs = new CircleShape();
cs.m_radius = box2d.scalarPixelsToWorld(r);
// Define a fixture
FixtureDef fd = new FixtureDef();
fd.shape = cs;
// Parameters that affect physics
fd.density = 100;
fd.friction = 10.3;
fd.restitution = 0.005;
body.createFixture(fd);
body.setLinearVelocity(new Vec2(0,0));
body.setAngularVelocity(0);
//Strength of force
}
Vec2 attract(Mover m) {
Vec2 pos = body.getWorldCenter();
Vec2 moverPos = m.body.getWorldCenter();
// Vector pointing from mover to attractor
Vec2 force = pos.sub(moverPos);
float distance = force.length();
// Keep force within bounds
distance = constrain(distance,10,50);
force.normalize();
float strength = ((G * 10 * m.body.m_mass) / (distance * distance)/2); // Calculate gravitional force magnitude
force.mulLocal(strength); // Get force vector --> magnitude * direction
return force;
}
void applyForce(Vec2 v) {
body.applyForce(v, body.getWorldCenter());
}
void display() {
// We look at each body and get its screen position
Vec2 pos = box2d.getBodyPixelCoord(body);
// Get its angle of rotation
float a = body.getAngle();
pushMatrix();
translate(pos.x,pos.y);
rotate(a);
fill(150);
stroke(0);
strokeWeight(1);
ellipse(0,0,r*2,r*2);
line(0,0,r,0);
popMatrix();
}
}
答案 0 :(得分:1)
供公众参考,最后我做了
Vec2 pos_s = box2d.getBodyPixelCoord(movers[s].body);
float res=dist(pos_s.x,pos_s.y,pos_i.x,pos_i.y);
//print(";"+res+";");
if ( res<100&res>-100 ) {
}