0
私はbox2dを使用しています。私は数字や楕円が画面上で跳ね返っていて、ある特定の距離内に入ると、楕円の間にジョイントを作りたいと思っています。私は私が持っている疑問は、これまでダウンBOX2D処理中の2つのボディ間の距離を求める方法
で世界に体の座標位置を決定する方法に出たとします
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();
}
}
あなたは "身体の位置を取得BOX2D" のような何かをグーグルで試したことがありますか? 1トンの有望な結果があるようです。 –