Java 如何在MT4J框架中设置组件重心,就像我提供链接的视频一样?
视频访问() 我想让组件的行为像这样,如果我拖动任何组件并将其放到屏幕上的任何位置,那么它应该像视频一样平滑地返回到屏幕的中心。我可以在屏幕上的任何特定位置设置任何组件,但无法在中间再次返回它们。 代码是Java 如何在MT4J框架中设置组件重心,就像我提供链接的视频一样?,java,Java,视频访问() 我想让组件的行为像这样,如果我拖动任何组件并将其放到屏幕上的任何位置,那么它应该像视频一样平滑地返回到屏幕的中心。我可以在屏幕上的任何特定位置设置任何组件,但无法在中间再次返回它们。 代码是 public class Physics extends AbstractScene { private float timeStep = 1.0f / 60.0f; private int constraintIterations = 10; private
public class Physics extends AbstractScene
{
private float timeStep = 1.0f / 60.0f;
private int constraintIterations = 10;
private int windowWidth=MT4jSettings.getInstance().windowWidth;
private int windowHeight= MT4jSettings.getInstance().windowHeight;
/** THE CANVAS SCALE **/
private float scale = 20;
private AbstractMTApplication app;
public World world;
private MTComponent physicsContainer;
PhysicsRectangle physRect;
PhysicsRectangle borderLeft, borderRight, borderTop, borderBottom;
Vector3D pos;
Vec2 gravity1;
AABB worldAABB;
public Physics(AbstractMTApplication mtApplication, String name)
{
super(mtApplication, name);
this.app = mtApplication;
float worldOffset = 600;
worldAABB = new AABB(new Vec2(-worldOffset, -worldOffset), new Vec2((app.width)/scale + worldOffset, (app.height)/scale + worldOffset));
gravity1 = new Vec2(0, 30);
boolean sleep = false;
//Create the pyhsics world
this.world = new World(worldAABB, gravity1, sleep);
this.registerGlobalInputProcessor(new CursorTracer(app, this));
this.registerPreDrawAction(new UpdatePhysicsAction(world, timeStep, constraintIterations, scale));
physicsContainer = new MTComponent(app);
physicsContainer.scale(scale, scale, 1, Vector3D.ZERO_VECTOR);
this.getCanvas().addChild(physicsContainer);
//Create borders around the screen
this.createScreenBorders(physicsContainer);
//Createa Rectangles
for (int i = 0; i < 5; i++)
{
pos = new Vector3D(windowWidth/2f, windowHeight/2f);
physRect = new PhysicsRectangle(pos, 100, 100, app, world, 1f, 0.4f, 0.4f, scale);
MTColor col = new MTColor(ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255),ToolsMath.getRandom(60, 255));
physRect.setFillColor(col);
physRect.setStrokeColor(col);
PhysicsHelper.addDragJoint(world, physRect, physRect.getBody().isDynamic(), scale);
physicsContainer.addChild(physRect);
}
}
private void createScreenBorders(MTComponent parent)
{
//Left border
float borderWidth = 50f;
float borderHeight = app.height;
Vector3D pos = new Vector3D(-(borderWidth/2f) , app.height/2f);
borderLeft = new PhysicsRectangle(pos, borderWidth, borderHeight, app, world, 0,0,0, scale);
borderLeft.setName("borderLeft");
parent.addChild(borderLeft);
//Right border
pos = new Vector3D(app.width + (borderWidth/2), app.height/2);
borderRight = new PhysicsRectangle(pos, borderWidth, borderHeight, app, world, 0,0,0, scale);
borderRight.setName("borderRight");
parent.addChild(borderRight);
//Top border
borderWidth = app.width;
borderHeight = 50f;
pos = new Vector3D(app.width/2, -(borderHeight/2));
borderTop = new PhysicsRectangle(pos, borderWidth, borderHeight, app, world, 0,0,0, scale);
borderTop.setName("borderTop");
parent.addChild(borderTop);
//Bottom border
pos = new Vector3D(app.width/2 , app.height + (borderHeight/2));
borderBottom = new PhysicsRectangle(pos, borderWidth, borderHeight, app, world, 0,0,0, scale);
borderBottom.setName("borderBottom");
parent.addChild(borderBottom);
}
public void onEnter() {
}
public void onLeave() {
}
}
我也试过什么?
1.我试图应用线程,但我们不能在MT4J中使用线程(我在一个网站上发现了这个),线程的替换是一个接口IPreDrawAction。我应用了这个,但还有一个问题,我们不能在物理组件上应用这个接口,所以我使用了MTComponent,但还有一个问题,我们不能在MTComponent上应用重力。这里有人可以帮助我吗?
public class staertWaterApp extends MTApplication
{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
initialize();
}
@Override
public void startUp()
{
addScene(new Physics(this, "Physics Example Scene"));
}
}