最近在学习物理引擎,看了几款,最终还是回到了JBOX2D,期中也遇到了些问题。问下大侠们,为什么创建多边形的时候该刚体就不会被模拟?一动也不动的,密度也设定了,大小也设定了,就是不动。除了圆形和长方形可以动其它什么三角形之类多边形都不动。我在想是不是因为形状的原因导致计算质量为0所以为静态物体???后来一看质量居然是负数,我的天类~~~~请教大侠这个问题怎么解决??? triange = new Polygon(world, 33, 33, 3, 10, 0.3f, 0.3f);
triange.add(new Vec2(10, 0));
triange.add(new Vec2(0, 10));
triange.add(new Vec2(20, 20));
triange.finish();我简单的封装了下。package model;import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;public class Polygon implements Drawable { private PolygonDef shape;
private BodyDef bodyDef;
private Body body;
private float[] vertices;
private Path path; public Polygon(World world, float x, float y, int vertices, float density,
float friction, float restitution) {
// TODO Auto-generated constructor stub
shape = new PolygonDef();
bodyDef = new BodyDef();
path = new Path();
shape.density = density;
shape.friction = friction;
shape.restitution = restitution;
shape.vertices.clear();
bodyDef.position.x = x;
bodyDef.position.y = y;
body = world.createBody(bodyDef);
this.vertices = new float[vertices << 1];
} @Override
public void paint(Canvas canvas, Paint paint) {
// TODO Auto-generated method stub
System.out.println("mass:"+body.getMass());
Vec2 vec2 = body.getPosition();
path.reset();
for (int i = 0, j = 0; i < shape.getVertexCount(); i++) {
vertices[j++] = shape.vertices.get(i).x + vec2.x;
vertices[j++] = shape.vertices.get(i).y + vec2.y;
if (i == 0)
path.moveTo(vertices[j - 2], vertices[j - 1]);
else
path.lineTo(vertices[j - 2], vertices[j - 1]);
}
path.close();
canvas.drawPath(path, paint);
} public PolygonDef getShape() {
return shape;
} public void setShape(PolygonDef shape) {
this.shape = shape;
} public BodyDef getBodyDef() {
return bodyDef;
} public void setBodyDef(BodyDef bodyDef) {
this.bodyDef = bodyDef;
} public Body getBody() {
return body;
} public void setBody(Body body) {
this.body = body;
}

public void add(Vec2 vec2){
shape.addVertex(vec2);
}

public void finish(){
body.createShape(shape);
body.setMassFromShapes();
}}

解决方案 »

  1.   

    真是晕啊,上面说错了其实是顺时针!
    不好意思,差点误导大家了~~~
    只因我猛的一看。原文:Convex polygon. The vertices must be in CCW order for a right-handed coordinate system with the z-axis coming out of the screen. Add vertices using PolygonDef.add(Vec2), 
    上面说CCW就是反时针counterclockwise,其实是顺时针(实践),由顶点,然后是右边一个点坐标一直到最左边接近顶点的坐标。换个小号来更正,一个号只能回复3次,CSDN真是够绝!
      

  2.   

    文档发我一份可以吗?[email protected] 楼主好人