3

project: I am making a simple project in which i want to be able to make a kinematic body and move it from point x to y. There doesn't seem to be much directly on "Creating kinematic bodies". i've been following a few different tutorials on implementing kinematic bodies because i couldnt find one that specified how to do it correctly.

Problem: It seems to be a simple fix but i just can't figure out what the problem is because i don't fully understand how to implement a kinematic body. i believe the problem lies in this code kinematicBody.setLinearVelocity(0.0f, 1.0f); because i get an error in "setLinearVelocity"stating "The method setLinearVelocity(float, float) is undefined for the type BodyDef" i feel that i am doing something wrong here

Outcome i want: i want to be able to create a kinematic body that moves from point x to point y.

here is my relevant code:

    package com.mohamed.JungleFighter;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
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.sun.xml.internal.ws.wsdl.writer.document.soap.Body;

public class JungleFighter extends Game {


  World world = new World(new Vector2(0, -100), true);  
  Box2DDebugRenderer debugRenderer;  
  OrthographicCamera camera;  
  static final float STEP=1/60f;  
  static final int VELOCITYITERATIONS=6;  
  static final int POSITIONITERATIONS=2;  
  static final float WORLD_TO_BOX=0.01f;  
  static final float BOX_WORLD_TO=100f;  
  @Override  
  public void create() {            
       camera = new OrthographicCamera();  
       camera.viewportHeight = 320;  
       camera.viewportWidth = 480;  
       camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);  
       camera.update(); 

 //test platform 3
           BodyDef kinematicBody =new BodyDef();  

       kinematicBody.type = BodyType.KinematicBody;
       kinematicBody.position.set(100, 250);

       PolygonShape shape = new PolygonShape();
       shape.setAsBox(50f, 25f);

       fixtureDef.shape = shape;
       fixtureDef.density = 0;
       fixtureDef.friction = 0;
       fixtureDef.restitution = 0;

       kinematicBody.setLinearVelocity(0.0f, 1.0f);


       // Create our body in the world using our body definition
       world.createBody(kinematicBody).createFixture(shape, 50.0f);
       shape.dispose();


       debugRenderer = new Box2DDebugRenderer();  
  }  
  @Override  
  public void dispose() {  
  }  
  @Override  
  public void render() {            
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  
       debugRenderer.render(world, camera.combined);  
       world.step(STEP, VELOCITYITERATIONS, POSITIONITERATIONS);  
  }  
  @Override  
  public void resize(int width, int height) {  
  }  
  @Override  
  public void pause() {  
  }  
  @Override  
  public void resume() {  


    }  
 }  
RooneyMUFC
  • 103
  • 2
  • 11

1 Answers1

3

It appears as if you aren't using the fixtureDef object anywhere. You are supposed to set it to the body once it is created.

The way you create a Kinematic Body isn't much different from a Static Body. The only difference would be how you define the type of the BodyDef.


Create a Kinematic Body It could be a StaticBody or DynamicBody too. Depends on what you want to accomplish.

BodyDef tempBodyDef = new BodyDef();
tempBodyDef.type = BodyType.KinematicBody;

Create the FixtureDefinition (The one that describes the properties of the fixture) Also create a shape and assign it to the fixture. You can modify the firction, isSensor, etc, properties of the tempFD to get the results you want.

FixtureDef tempFD = new FixtureDef();
PolygonShape tempShape = new PolygonShape();
tempFD.shape = tempShape;

The center of the shape for the body will be at 0,0

Vector2 tempVector = new Vector2();
tempVector.x = 0;
tempVector.y = 0;

Setting the size and position of the body's shape. Notice I'm using half the width and height, as that is what the setAsBox function requires.

tempShape.setAsBox(myWidth/2f, myHeight/2f, tempVector, myAngle);
tempBodyDef.position.set(myXPosition, myYPosition);

Create the body and assign the fixture to it

Body testBody = world.createBody(tempBodyDef);
testBody.createFixture(tempFD);

Don't forget to dispose of the Shape once you are done with it.

tempShape.dispose();

And you are done.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
cavpollo
  • 4,071
  • 2
  • 40
  • 64
  • 1
    thanks @cavpollo, that worked, how do i get the kinematic body to move from point x to y? "kinematicBody.setLinearVelocity(-200.0f, 200.0f);" still doesnt work i still get the error saying "The method setLinearVelocity(float, float) is undefined for the type BodyDef" – RooneyMUFC Jun 26 '14 at 11:19
  • 1
    @RooneyMUFC, the setLinearVelocity method should be called on the Body (which is the testBody variable in the above example), not on the BodyDef, which is just a temporary convenience class to describe the body properties. – Will Kru Jun 26 '14 at 14:16
  • @WillKru i tried doing Body testBody = world.createBody(tempBodyDef); testBody.createFixture(tempFD); but that doesnt seem to work for my program so i modified it to world.createBody(testBody).createFixture(tempshape, 50.0f); which compiled – RooneyMUFC Jun 26 '14 at 14:21
  • when i try "Body testBody = world.createBody(kinematicBody);" i get this error "Type mismatch: cannot convert from com.badlogic.gdx.physics.box2d.Body to com.sun.xml.internal.ws.wsdl.writer.document.soap.Body" – RooneyMUFC Jun 26 '14 at 14:25
  • Make sure your imports are right. At the top of your document replace com.sun.xml.internal.ws.wsdl.writer.document.soap.Body with com.badlogic.gdx.physics.box2d.Body. LibGDX does support the createFixture(FixtureDef) method, so probably there's something wrong at your end. I would recommend visiting the libGDX IRC channel if you are in need of immediate assistance, the folks there are really helpful. – Will Kru Jun 26 '14 at 14:33