gpt4 book ai didi

android - jBox2d android 绘制动态物体

转载 作者:行者123 更新时间:2023-11-29 00:49:49 27 4
gpt4 key购买 nike

我从教程 here 开始并为 View 添加了绘图代码(覆盖整个屏幕)。即使我正在调用 world.step(,),场景仍然是静态的而不是动态的。如何使场景动态化?我的绘图是正确的实现还是有更好的方法(使用 jBox2d 函数)?

private class PhysicsView extends View {

Paint mPaint;

public PhysicsView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL_AND_STROKE);
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

synchronized (canvas) {
Body[] b = mWorld.bodies;
for (int i = 0; i < b.length; i++) {
if (b[i] != null) {

Float mass = b[i].getMass();
Vec2 v = b[i].getPosition();
canvas.drawCircle(v.x, v.y, 15, mPaint);
}
}
Paint p2 = new Paint(mPaint);
p2.setColor(Color.GREEN);
Vec2 base = mWorld.groundBody.getPosition();
canvas.drawRect(new RectF((float) base.x - 25.0f,
(float) base.y - 10.0f, (float) base.x + 25.0f,
(float) base.y + 10.0f), p2);

canvas.drawLine(0.0f, 0.0f,
mWorld.world.getWorldAABB().upperBound.x, mWorld.world
.getWorldAABB().upperBound.y, p2);
}
}
}


public class PhysicsWorld {

public int targetFPS = 40;
public int timeStep = (1000 / targetFPS);
public int iterations = 5;

public Body[] bodies = new Body[50];
public Body groundBody;
private int count = 0;

private AABB worldAABB;
public World world;
private BodyDef groundBodyDef;
private PolygonDef groundShapeDef;

private Vec2 screenDimensions;

public void create(Vec2 dimens) {

screenDimensions = dimens;

worldAABB = new AABB();
worldAABB.lowerBound.set(new Vec2((float) -1*dimens.x, (float) -1*dimens.y));
worldAABB.upperBound.set(new Vec2((float) dimens.x, (float) dimens.y));

Vec2 gravity = new Vec2((float) 0.0, (float) 0.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);

groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) dimens.x/2, (float) dimens.y - 10.0f));
groundBody = world.createBody(groundBodyDef);
groundBody.m_mass = 200.0f;

groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) 50.0, (float) 10.0);
groundShapeDef.density = 1.0f;
groundShapeDef.friction = 0.3f;
groundBody.createShape(groundShapeDef);

}

public void addBall() {

float x = (float) Math.random() * screenDimensions.x;
float y = (float) Math.random() * screenDimensions.y;

BodyDef bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) x, (float) y));
bodyDef.massData.mass = 20.0f;
bodies[count] = world.createBody(bodyDef);
bodies[count].m_type = Body.e_dynamicType;
bodies[count].m_linearVelocity = new Vec2(1.0f, 2.0f);

CircleDef circle = new CircleDef();
circle.radius = (float) 1.8;
circle.density = (float) 1.0;
circle.type = ShapeType.CIRCLE_SHAPE;

bodies[count].createShape(circle);

count++;
}

public void update() {
world.step(timeStep, iterations);

}

public void changeGravity(Vec2 gravity) {
world.setGravity(gravity);
}
}

最佳答案

您的代码存在许多问题....但这个答案可能为时已晚,无论如何都无法提供任何帮助。

首先,world.createBody() 似乎已被弃用。现在你必须在我拥有的最新版本的 Android Box2D jar 中显式调用 world.createDynamicBody()。

无论如何……我的版本是这样的……不完美,但至少它是动态的。

package com.box2D;

import java.util.ArrayList;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.CircleDef;
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.util.Log;

public class PhysicsWorld
{
public float timeStep = 1;
public int iterations = 1;

private ArrayList<Body> bodies;
private int count = 0;

private AABB worldAABB;
private World world;
private BodyDef groundBodyDef;
private PolygonDef boxShapeDef;

//a paint
private Paint paint;

public void create()
{
//create the paint
paint = new Paint();
paint.setColor(0xFFFFFF);

// Step 1: Create Physics World Boundaries
worldAABB = new AABB();
worldAABB.lowerBound.set(new Vec2((float) -100.0, (float) -100.0));
worldAABB.upperBound.set(new Vec2((float) 100.0, (float) 300.0));

// Step 2: Create Physics World with Gravity
Vec2 gravity = new Vec2((float) 0.0, (float)10.0);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);
bodies = new ArrayList<Body>();

// Step 3: Create Ground Box
groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) 300.0));
Body groundBody = world.createDynamicBody(groundBodyDef);
boxShapeDef = new PolygonDef();
boxShapeDef.setAsBox((float) 50.0, (float) 10.0);
groundBody.createShape(boxShapeDef);
}

public void addBall()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float)16.0+(count * 10), (float)24.0);
Body newBody = world.createDynamicBody(bodyDef);

// Create Shape with Properties
CircleDef circle = new CircleDef();
circle.radius = (float)5;
circle.density = (float)1.0;

// Assign shape to Body
newBody.createShape(circle);
newBody.setMassFromShapes();
newBody.m_userData = "Circle";
bodies.add(newBody);

// Increase Counter
count += 1;
}

public void addBox()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float)36.0+(count * 10), (float)24.0);
Body newBody = world.createDynamicBody(bodyDef);

// Create Shape with Properties
boxShapeDef = new PolygonDef();
boxShapeDef.setAsBox((float) 50.0, (float) 10.0);

// Assign shape to Body
newBody.createShape(boxShapeDef);
newBody.setMassFromShapes();
newBody.m_userData = "Box";
bodies.add(newBody);

// Increase Counter
count += 1;
}

/*
*
*
* Physics Update
*/

private Vec2 position;
private float angle;

public void Update(Canvas canvas)
{
// Update Physics World
world.step(timeStep/5, 1);

// Print info of latest body
for (Body i : bodies)
{
position = i.getPosition();
angle = i.getAngle();

if(i.m_userData == "Circle")
canvas.drawCircle(position.x, position.y, 5,paint );

if(i.m_userData == "Box")
canvas.drawRect((float)(position.x -5), (float)(position.y - 5), (float)(position.x + 5), (float)(position.y + 5), paint );

Log.v("Physics Test", "Pos: (" + position.x + ", " + position.y + "), Angle: " + angle);
}
}
}

关于android - jBox2d android 绘制动态物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com