2017-04-03 89 views
0

我正在使用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

你有沒有試着用搜索引擎什麼 「的Box2D得到體位」?似乎有很多有希望的結果。 –

回答

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) { 
} 
相關問題