LibGDX/Box2D UnsatisfiedLinkError

假如想象 提交于 2019-12-10 11:19:53

问题


I'm using the EXACT same code as used in this tutorial but for some reason I get an error while trying to run the project. The default project for LibGDX worked fine.

Error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.World.newWorld(FFZ)J
at com.badlogic.gdx.physics.box2d.World.newWorld(Native Method)
at com.badlogic.gdx.physics.box2d.World.<init>(World.java:222)
at net.ocps.tchs.permarun.PermaRun.<init>(PermaRun.java:19)
at net.ocps.tchs.permarun.Main.main(Main.java:14)

Line it is referring to(the first line in the class):

World world = new World(new Vector2(0, -100), true); 

Edit: Upon further debugging, I commented out anything involving the the world variable and the program ran (nothing showed up except a black box though because I commented out anything that errors upon commenting out the world definition line.)

Edit2: Full code that works.(There are different ways of using GdxNativesLoader.load(); but this is the way I decided to use, thanks to aquaraga)

import com.badlogic.gdx.ApplicationListener;  
import com.badlogic.gdx.Gdx;  
import com.badlogic.gdx.graphics.GL10;  
import com.badlogic.gdx.graphics.OrthographicCamera;  
import com.badlogic.gdx.math.Vector2;  
import com.badlogic.gdx.physics.box2d.Body;  
import com.badlogic.gdx.physics.box2d.BodyDef;  
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;  
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;  
import com.badlogic.gdx.physics.box2d.CircleShape;  
import com.badlogic.gdx.physics.box2d.FixtureDef;  
import com.badlogic.gdx.physics.box2d.PolygonShape;  
import com.badlogic.gdx.physics.box2d.World;  
import com.badlogic.gdx.utils.GdxNativesLoader;
public class PermaRun implements ApplicationListener {  
     World world;  
     Box2DDebugRenderer debugRenderer;  
     OrthographicCamera camera;  
     static final float BOX_STEP=1/60f;  
     static final int BOX_VELOCITY_ITERATIONS=6;  
     static final int BOX_POSITION_ITERATIONS=2;  
     static final float WORLD_TO_BOX=0.01f;  
     static final float BOX_WORLD_TO=100f;  
     @Override  
     public void create() {
          GdxNativesLoader.load();
          world = new World(new Vector2(0.0f, -100.0f), true);
          camera = new OrthographicCamera();  
          camera.viewportHeight = 320;  
          camera.viewportWidth = 480;  
          camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);  
          camera.update();  
          //Ground body  
          BodyDef groundBodyDef =new BodyDef();  
          groundBodyDef.position.set(new Vector2(0, 10));  
          Body groundBody = world.createBody(groundBodyDef);  
          PolygonShape groundBox = new PolygonShape();  
          groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);  
          groundBody.createFixture(groundBox, 0.0f);  
          //Dynamic Body  
          BodyDef bodyDef = new BodyDef();  
          bodyDef.type = BodyType.DynamicBody;  
          bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2);  
          Body body = world.createBody(bodyDef);  
          CircleShape dynamicCircle = new CircleShape();  
          dynamicCircle.setRadius(5f);  
          FixtureDef fixtureDef = new FixtureDef();  
          fixtureDef.shape = dynamicCircle;  
          fixtureDef.density = 1.0f;  
          fixtureDef.friction = 0.0f;  
          fixtureDef.restitution = 1;  
          body.createFixture(fixtureDef);  
          debugRenderer = new Box2DDebugRenderer();  
     }  
     @Override  
     public void dispose() {  
     }  
     @Override  
     public void render() {            
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
          debugRenderer.render(world, camera.combined);  
          world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);  
     }  
     @Override  
     public void resize(int width, int height) {  
     }  
     @Override  
     public void pause() {  
     }  
     @Override  
     public void resume() {  
     }  

}

回答1:


You may want to try GdxNativesLoader.load();. Do it in a static manner, i.e. when JVM loads the class:

static {
    GdxNativesLoader.load();
}

Apparently, the link error happens when your code or java library invokes some native code.



来源:https://stackoverflow.com/questions/18495708/libgdx-box2d-unsatisfiedlinkerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!