2016-03-22 23 views
0

Shiffman öğreticilerinden İşleme için box2d kullanıyorum. Tek istediğim, belirli bir x koordinatına geldiğinde kutumun pozisyonunu değiştirmek. Bu, kutu sınıfındaki x koordinatının box2D dünyasından çevrilmesinin, aşağıda wrap fonksiyonunda manipüle edilmesi ve daha sonra box2d dünyasına geri döndürülmesi kadar kolay değildir.Kutu2d şeklinin konumu nasıl değiştirilir? İşlem

Aşağıdaki kodu aşağıdan oluşturdum. Sahip olduğum sorun, kutunun konumunu veya kutuyu tanımlayan gövdeyi değiştirmek için döndürülen x koordinatının nasıl uygulanacağıdır. Bir MCVE sağlamaya çalıştım ama box2d kodu şişiriyor. Birisi biliyorsa tam bir taslak yayınlamak için bir yer olsaydı harika olurdu. pencerenizin Dünya sınırları dışında

import controlP5.*; 
import shiffman.box2d.*; 
import org.jbox2d.collision.shapes.*; 
import org.jbox2d.common.*; 
import org.jbox2d.dynamics.*; 
import org.jbox2d.particle.*; 
import org.jbox2d.pooling.*; 
import javax.swing.*; 
import java.awt.Frame; 
import java.awt.MouseInfo; 
import java.awt.Point; 
//import org.jbox2d.p5.*; 
PApplet s; 
// A reference to our box2d world 
Box2DProcessing box2d; 
ControlP5 cp5; 
Body tbb; 
// A list we'll use to track fixed objects 
ArrayList<Boundary> boundaries; 
// A list for all of our rectangles 
ArrayList<Box> boxes; 
ArrayList<Box> boxes_Clone1; 
ArrayList<Box> boxes_Clone2; 
ArrayList<Box> boxes_Clone3; 
ArrayList<Box> boxes_Clone4; 
PFont f; 
String str; 
float num; 


void setup() { 
    size(840,860); 
    f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on 

    cp5 = new ControlP5(this); 

box2d = new Box2DProcessing(this); 
    box2d.createWorld(); 
    // We are setting a custom gravity 
    box2d.setGravity(0, + 0.1); 
} 

void draw() { 
box2d.step(); 

for (int i = boxes.size()-1; i >= 0; i--) { 
    Box b = boxes.get(i); 
    //b.scale(2); 
    b.wrap(); 
    if (b.done()) { 
     //boxes.remove(i); 
    } 
    // 
    } 

class Box { 

    // We need to keep track of a Body and a width and height 
    Body body; 

    float w; 
    float h; 

    public color col; 
    public float den; 
    public float rest; 
    public float fric; 
public float linVecx; 
public float linVecy; 
    // Constructor 
    Box(float x, float y,color col,float fric,float rest,float den,float linVecx,float linVecy) { 
    this.w = 8; 
    this.h = 8; 
    this.col = col; 
    this.fric=fric; 
    this.rest=rest; 
    this.den=den; 
    this.linVecx=linVecx; 
    this.linVecy=linVecy; 

    // Add the box to the box2d world 
    makeBody(new Vec2(x, y), w, h); 
    } 
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(); 
    rectMode(CENTER); 
    pushMatrix(); 
    translate(pos.x, pos.y); 
    rotate(-a); 
    fill(col); 
    noStroke(); 
    //strokeWeight(2); 
    ellipse(0, 0, w, h); 
    popMatrix(); 
    } 

    // This function adds the rectangle to the box2d world 
    void makeBody(Vec2 center, float w_, float h_) { 

    // Define a polygon (this is what we use for a rectangle) 
    PolygonShape sd = new PolygonShape(); 
    float box2dW = box2d.scalarPixelsToWorld(w_/2); 
    float box2dH = box2d.scalarPixelsToWorld(h_/2); 
    sd.setAsBox(box2dW, box2dH); 

    // Define a fixture 
    FixtureDef fd = new FixtureDef(); 
    fd.shape = sd; 
    // Parameters that affect physics 
    fd.density = den; 
    fd.friction = fric; 
    fd.restitution = rest; 

    // Define the body and make it from the shape 
    BodyDef bd = new BodyDef(); 
    bd.type = BodyType.DYNAMIC; 
    bd.linearDamping = 3.0f; 
    bd.position.set(box2d.coordPixelsToWorld(center)); 

    body = box2d.createBody(bd); 
    body.createFixture(fd); 

    // Give it some initial random velocity 
    body.setLinearVelocity(new Vec2(linVecx, linVecy)); 
    body.setAngularVelocity(random(-5, 5)); 

    } 
    float wrap(){ 
    Vec2 pos = box2d.getBodyPixelCoord(body); 
    if (pos.x > 590) { 
     pos.x = 300; 
     return box2d.scalarPixelsToWorld(pos.x); 
     //bd.position = box2d.coordPixelsToWorld(pos.x,pos.y); 

    } 
    return 0; 
} 

cevap

0

Şekil. Yani o ederek, aşağıdaki gibi olacaktır:

  • bir nesne dünyada olduğu sol üst köşesinde bir pencerede (konum 0,0), var?
  • Bir nesne sağ üst köşede (konum width,0) pencerede olduğunda, dünyada nerede?
  • Bir nesne sol alt köşede (konum 0,height) pencerede olduğunda, dünyada nerede?
  • Bir nesne sağ alt köşede (konum width,height) pencerede olduğunda, dünyada nerede?

Bu değerlere sahip olduğunuzda, tüm sınırlarınızı dünya alanında kontrol edebilirsiniz.

Kutu2D'nin acemiler için gerçekten tasarlanmadığını not etmeliyim. Box2D'ye gerçekten hazır olabilmek için OOP ve veri yapılarına çok aşina olmanız gerekir. Daniel Shiffman çok iyi bir öğretmendir, ancak bir fizik kütüphanesi kullanmaya başlamadan önce biraz daha basit bir şey denemek ve geri dönmek isteyebilirsiniz.

+0

Merhaba Kevin. Nesnemin dünyayla ilgili olduğunu kontrol etmenin farkına varıyorum. Benim sorduğum bu değildi. Kutunun x koordinatlarını nasıl alacağımı soruyordum. –

+0

@SebastianZeki Bu kodu zaten yapıyor musunuz? Vec2 pos = box2d.getBodyPixelCoord (gövde); if (pos.x> 590) {' –

+0

Hayır, vücudun x koordinatı budur. Evet, kutunun nerede olduğunu tanımlar ancak pos.x dosyasını kutunun konumu olarak nasıl dönüştürürüm? –

İlgili konular