Box2D Forums

It is currently Tue Jun 30, 2015 11:30 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed May 29, 2013 11:08 am 
Offline

Joined: Wed May 29, 2013 10:36 am
Posts: 1
Good day everyone.

I asking your help to figure out, what working wrong with setLinearVelocity method. I've browsed many tutorial and code has been written in accordance with it. I can't understand what i'am doing wrong,

Thing is, body on which method I use, not moving or moving inadequately instructions. In addition, the body stops moving quickly, although I removed the friction.

Please, help!

Code:
/////////////////////Creating a player/////////////////////Creating a player/////////////////////Creating a player/////////////////////

public class Alex {
  private static final int FRAME_COLS = 8; // #1
  private static final int FRAME_ROWS = 8; // #2


  Texture         walkSheet; // #9
  TextureRegion[] walkDown, walkLeft, walkRight, walkUp; // #5
  Animation       walkDownAnimation, walkLeftAnimation, walkRightAnimation, walkUpAnimation;
  SpriteBatch     spriteBatch = new SpriteBatch(); // #12
  TextureRegion   currentFrame; // #7
  public Fixture playerPhysicsFixture;

  Rectangle bounds;
  Vector2 velocityAlex;

  Vector2 mousePoint;
  float  stateTime ,size;
  Body box;
  public enum State {STAY, WALKING, SIT}
  State state;

  public static final float SPEED = 150f;
  public int HEIGHT;
  public int WIDTH;


  public Alex(Vector2 pos, World world) {   

    BodyDef def = new BodyDef();
    def.type = BodyType.DynamicBody;
    def.position.set(pos);
   
    PolygonShape alexShape = new PolygonShape();
    alexShape.setAsBox(128, 256);
   
    FixtureDef alexFix = new FixtureDef();
    alexFix.shape = alexShape;
    alexFix.density = 1f;
    alexFix.restitution = 0.1f;
    alexFix.friction = 0.2f;
 
   
    box = world.createBody(def);
    box.createFixture(alexFix);
    playerPhysicsFixture = box.getFixtureList().get(0);
   
    size = 0.7f;
    stateTime= 0f;

    mousePoint = new Vector2();
    mousePoint.x=-1;
    mousePoint.y=-1;
    state= State.STAY;

    velocityAlex = new Vector2();


    walkSheet = new Texture(Gdx.files.internal("data/AnimationaSpreadsheet-StepForward_2.png"));
    HEIGHT = walkSheet.getHeight()/FRAME_ROWS;
    WIDTH = walkSheet.getWidth()/ FRAME_COLS;
    TextureRegion tmp[][] = TextureRegion.split(walkSheet,walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);


Code:

                 ............Animation creating............................
    box.setBullet(true);
    box.setUserData(this);
  }

  public Body getBody()
  {
    return box;
  }

  public Rectangle getBounds() {
    return bounds;
  }

  public Vector2 getVelocity() {
    return velocityAlex;
  }

  public Vector2 getPosition() {
    return box.getPosition();
  }

  public Animation getwalkDownAnimation(){
    return walkDownAnimation;
  }

  public Animation getwalkLeftAnimation(){
    return walkLeftAnimation;
  }

  public Animation getwalkRightAnimation(){
    return walkRightAnimation;
  }

  public Animation getwalkUpAnimation(){
    return walkUpAnimation;
  }

  public void update(float delta) {
    System.out.println("They went into the update, update motion");
    System.out.println("Old coordinate. х="+box.getPosition().x+" y="+box.getPosition().y);
    System.out.println("Velocity х="+velocityAlex.x+" y="+velocityAlex.y);
    //positionAlex.add(velocityAlex.scl(delta));
    System.out.println(" ");
    System.out.println("==============================================================");
    System.out.println("Velocity  х="+velocityAlex.scl(delta).x+" y="+velocityAlex.scl(delta).y);
    System.out.println("==============================================================");
    System.out.println(" ");
    System.out.println("Getting linear speed:"+box.getLinearVelocity());
    box.setLinearVelocity(velocityAlex.x*100, velocityAlex.y*100);    //alex.getBody().setLinearVelocity(alex.velocityAlex);
    System.out.println("Linear speed after changing:"+box.getLinearVelocity());

    System.out.println("Updated coordintes. х="+box.getPosition().x+" y="+box.getPosition().y);
  }

  public void setFriction(float f){
    playerPhysicsFixture.setFriction(f);
  }
 
  void MousePointReset()
  {
    mousePoint.x=-1;
    mousePoint.y=-1;
  }

  public void Disporse()
  {
    box.destroyFixture(playerPhysicsFixture);
   
  }
}



Code:
////////////////////////////////////////World creating////////////////////////////////////////World creating///////////////////////////////////////
public class level {
 
 
  Array<Brick> bricks = new Array<Brick>();
  public World world;
  public Alex alex;
  public Location location;
  public int width;


  public int height;


  public Array<Brick> getBricks() {
  return bricks;
  }
 

  public Alex getPlayer() {
  return alex;
  }
 
  public Location getLocation()
  {return location;}

  public level() {
  world = new World(new Vector2(0, 0), true);
  MyContactListener MCL = new MyContactListener(world);
  world.setContactListener(MCL);
  createWorld();
  }


  public void createWorld() {
    System.out.println("Вошли в createWorld");


    alex = new Alex(new Vector2(200,500),world);   

    alex.getBody().setTransform(alex.getPosition(), 0);
    alex.getBody().setFixedRotation(true);

    location = new Location();
    location.makeLocationMap();
    int numbOfBrick=0;
    for(int i=0; i<4; i++)
    {
      for(int j=0; j<4; j++)
      {
        switch(location.getMatrix()[i][j])
        {
        case 1:
        {
          bricks.add(new Brick(new Vector2(i*256, j*256),location.box.getTextureData()));
          bricks.get(numbOfBrick).brickBody = createBox(BodyType.StaticBody,bricks.get(numbOfBrick).WIDTH, bricks.get(numbOfBrick).WIDTH, 2, bricks.get(numbOfBrick).position);
          break;
        }
        case 0:
        {
          bricks.add(new Brick(new Vector2(i*256, j*256),location.texture.getTextureData()));
          break;
        }
        };
        numbOfBrick++;
      }
    }
    System.out.println("Exit form createWorld");
  }
 
   
  private Body createBox(BodyType type, float width, float height, float density, Vector2 vector) {
    BodyDef def = new BodyDef();
    def.type = type;
    Body box = world.createBody(def);
    PolygonShape poly = new PolygonShape();
    poly.setAsBox(width, height);   
    box.createFixture(poly, density);
    poly.dispose();
   
    box.setTransform(vector, 0);
    return box;
  }
 
  public void dispose()
  {
    bricks.clear();
    world.dispose();
  }
 
  public World getWorld()
  {
    return world;
  }
}


Code:


////////////////////////////////////////////////////controller////////////////////////////////////////////////////controller////////////////////////////////////////////////

public class controller{


  enum Keys {LEFT, RIGHT, UP, DOWN}


  public Alex alex;
  level world;
  static Map<Keys, Boolean> keys = new HashMap<controller.Keys, Boolean>();

  static {
    keys.put(Keys.LEFT, false);
    keys.put(Keys.RIGHT, false);
    keys.put(Keys.UP, false);
    keys.put(Keys.DOWN, false);
  };


  public controller(level world) {
    this.alex = world.getPlayer();
    this.world = world;
  }

  public void leftPressed() {
    System.out.println("Жмакнули левую кнопень");
    keys.get(keys.put(Keys.LEFT, true));
  }


  public void rightPressed() {
    keys.get(keys.put(Keys.RIGHT, true));
  }

  public void upPressed() {
    keys.get(keys.put(Keys.UP, true));
  }


  public void downPressed() {
    System.out.println("Жмакнули кнопень вниз");
    keys.get(keys.put(Keys.DOWN, true));
  }



  public void leftReleased() {
    keys.get(keys.put(Keys.LEFT, false));
  }
  public void rightReleased() {
    keys.get(keys.put(Keys.RIGHT, false));
  }


  public void upReleased() {
    keys.get(keys.put(Keys.UP, false));
  }


  public void downReleased() {
    keys.get(keys.put(Keys.DOWN, false));
  }


  public void update(float delta) {
    isInContact = isPlayerpressAgainst(Gdx.graphics.getDeltaTime());
    processInput();
    world.getPlayer().update(delta);
  }

  public void resetWay(){
    rightReleased();
    leftReleased();
    downReleased();
    upReleased();
  }

  private boolean isPlayerpressAgainst(float deltaTime) {           
    List<Contact> contactList = world.getWorld().getContactList();
    for(int i = 0; i < contactList.size(); i++) {
      Contact contact = contactList.get(i);
      if(contact.isTouching())
      {
        if(contact.getFixtureA() == world.getPlayer().playerPhysicsFixture ||
            contact.getFixtureB() == world.getPlayer().playerPhysicsFixture)
        {             

          System.out.println("contact.getFixtureA().getUserData()="+contact.getFixtureA().getUserData());

          WorldManifold manifold = contact.getWorldManifold();
          boolean isContact = false;

          if(manifold.getNumberOfContactPoints() > 0)
          {
            isContact = true;

          }
          else
          {
            isContact = false;
          }

          if(isContact) {
            //if(contact.getFixtureA().getUserData() != null/* && contact.getFixtureA().getUserData(). equals("p")*/)
            /*{
              System.out.println("======================================================");
              //world.groundedPlatform =  (MovingPlatform)contact.getFixtureA(). getBody().getUserData(); 
              if (!keys.get(Keys.LEFT) && !keys.get(Keys.RIGHT))
                contact.setFriction(1000F);
              else
                contact.setFriction(0F);
             
              System.out.println("======================================================");
            }
            return true;           
          }
          else
          {
            return false;
          }
        }
      }
    }
    return false;
  }
  public static boolean isInContact;
 

  private void processInput() {

    System.out.println("Alex.x="+alex.getPosition().x+" Alex.y="+alex.getPosition().y);
    System.out.println("mousePoint.x="+alex.mousePoint.x+" mousePoint.y="+alex.mousePoint.y);
    if (keys.get(Keys.LEFT))
    {
      alex.getVelocity().x = alex.mousePoint.x - alex.getPosition().x;
      alex.getVelocity().x =alex.getVelocity().x - alex.WIDTH/2;
    }

    if (keys.get(Keys.RIGHT))
    {
      alex.getVelocity().x = alex.mousePoint.x - alex.getPosition().x;
      alex.getVelocity().x =alex.getVelocity().x - alex.WIDTH/2;
    }

    if (keys.get(Keys.UP))
    {
      alex.getVelocity().y = (1024-alex.mousePoint.y) - alex.getPosition().y;
      alex.getVelocity().y=alex.getVelocity().y-alex.HEIGHT/10;
    }

    if (keys.get(Keys.DOWN))
    {
      alex.getVelocity().y = (1024-alex.mousePoint.y) - alex.getPosition().y;
      alex.getVelocity().y=alex.getVelocity().y-alex.HEIGHT/10;
      System.out.println("Обрабатываем кнопку 'вниз'. alex.getVelocity().y="+alex.getVelocity().y);
    }


    if ((keys.get(Keys.LEFT) && keys.get(Keys.RIGHT)) || (!keys.get(Keys.LEFT) && (!keys.get(Keys.RIGHT))))
    {
      if(!isInContact)
        alex.setFriction(0F); 
      else{
        if (!keys.get(Keys.LEFT) && !keys.get(Keys.RIGHT))
          alex.setFriction(2F);
        else
          alex.setFriction(0.2F);
      }
      alex.getVelocity().x = 0;
      System.out.println("Обнуляем смещение по Х");
    }
    if ((keys.get(Keys.UP) && keys.get(Keys.DOWN)) || (!keys.get(Keys.UP) && (!keys.get(Keys.DOWN))))
    {
      if(!isInContact)
        alex.setFriction(0F); 
      else{
        if (!keys.get(Keys.UP) && (!keys.get(Keys.DOWN)))
          alex.setFriction(2F);
        else
          alex.setFriction(0.2F);
      }
      alex.getVelocity().y = 0;
    }
  }
}


In log, you can see an output of programm. As you can see, even after directly set linear velocity, the figures changes for unknown reasons.
Code:
============================

contact.getFixtureA().getUserData()=null
Alex.x=195.60007 Alex.y=500.0
mousePoint.x=-1.0 mousePoint.y=-1.0

They went into the update, update motion
Old coordinate. х=195.60007 y=500.0
Velocity х=0.0 y=0.0
 
==============================================================
Velocity  х=0.0 y=0.0
==============================================================
 
Getting linear speed:[0.0:0.0]
Linear speed after changing:[0.0:0.0]
Updated coordintes. х=195.60007 y=500.0
Вошли в ChangeNavigation.

 
============================

contact.getFixtureA().getUserData()=null
Alex.x=195.20007 Alex.y=500.0
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=522.7999
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=104.0
They went into the update, update motion
Old coordinate. х=195.20007 y=500.0
Velocity х=522.7999 y=104.0
 
==============================================================
Velocity  х=48.88666 y=0.909375
==============================================================
 
Getting linear speed:[0.0:0.0]
Linear speed after changing:[457.13577:90.9375]
Updated coordintes. х=195.20007 y=500.0
 
...
 
============================

contact.getFixtureA().getUserData()=null
Alex.x=193.90181 Alex.y=501.78693
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=524.0982
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=102.21307
They went into the update, update motion
Old coordinate. х=193.90181 y=501.78693
Velocity х=524.0982 y=102.21307
 
==============================================================
Velocity  х=56.26308 y=1.1779535
==============================================================
 
Getting linear speed:[-8.367544:16.645456]
Linear speed after changing:[603.9964:117.79535]
Updated coordintes. х=193.90181 y=501.78693
 
...
 
============================

contact.getFixtureA().getUserData()=null
Alex.x=192.58928 Alex.y=503.56662
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=525.4107
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=100.43338
They went into the update, update motion
Old coordinate. х=192.58928 y=503.56662
Velocity х=525.4107 y=100.43338
 
==============================================================
Velocity  х=66.8184 y=1.6243228
==============================================================
 
Getting linear speed:[-7.175504:13.994136]
Linear speed after changing:[849.75397:162.43228]
Updated coordintes. х=192.58928 y=503.56662
 
...
 
============================

contact.getFixtureA().getUserData()=null
Alex.x=191.26219 Alex.y=505.33878
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=526.7378
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=98.661224
They went into the update, update motion
Old coordinate. х=191.26219 y=505.33878
Velocity х=526.7378 y=98.661224
 
==============================================================
Velocity  х=56.444424 y=1.1329209
==============================================================
 
Getting linear speed:[-8.651557:16.537636]
Linear speed after changing:[604.84985:113.29208]
Updated coordintes. х=191.26219 y=505.33878
 
...
 
============================

contact.getFixtureA().getUserData()=null
Alex.x=189.92026 Alex.y=507.1031
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=528.0797
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=96.89691
They went into the update, update motion
Old coordinate. х=189.92026 y=507.1031
Velocity х=528.0797 y=96.89691
 
==============================================================
Velocity  х=52.746964 y=0.9667316
==============================================================
 
Getting linear speed:[-9.430241:17.663418]
Linear speed after changing:[526.8603:96.673164]
Updated coordintes. х=189.92026 y=507.1031
 
...
 
============================
contact.getFixtureA().getUserData()=null
Alex.x=188.56319 Alex.y=508.85922
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=529.4368
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=95.14078
They went into the update, update motion
Old coordinate. х=188.56319 y=508.85922
Velocity х=529.4368 y=95.14078
 
==============================================================
Velocity  х=60.043816 y=1.2237
==============================================================
 
Getting linear speed:[-8.439036:15.484717]
Linear speed after changing:[680.9613:122.37]
Updated coordintes. х=188.56319 y=508.85922
 
...
 
============================
contact.getFixtureA().getUserData()=null
Alex.x=187.19067 Alex.y=510.60684
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=530.8093
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=93.39316
They went into the update, update motion
Old coordinate. х=187.19067 y=510.60684
Velocity х=530.8093 y=93.39316
 
==============================================================
Velocity  х=58.747673 y=1.1439823
==============================================================
 
Getting linear speed:[-8.7870865:15.790556]
Linear speed after changing:[650.1938:114.39823]
Updated coordintes. х=187.19067 y=510.60684
 
...
 
============================

contact.getFixtureA().getUserData()=null
Alex.x=185.80243 Alex.y=512.34564
mousePoint.x=846.0 mousePoint.y=369.0
Обрабатываем кнопку 'вправо'. alex.getVelocity().x=532.1976
Обрабатываем кнопку 'вниз'. alex.getVelocity().y=91.65436
They went into the update, update motion
Old coordinate. х=185.80243 y=512.34564
Velocity х=532.1976 y=91.65436
 
==============================================================
Velocity  х=59.311974 y=1.1383927
==============================================================
 
Getting linear speed:[-8.867445:15.601818]
Linear speed after changing:[661.01587:113.83927]
Updated coordintes. х=185.80243 y=512.34564


Top
 Profile  
 
PostPosted: Tue Jun 02, 2015 12:29 am 
Offline

Joined: Tue May 26, 2015 10:53 pm
Posts: 3
From all that I have read thought and seen I still doubt that there is some flaw with the coding somewhere that we all are failing to notice. It must be something so silly and simple that is messing up with your work. I shall refer with a friend and if we do find it, I would surely let you know as soon as possible.


please do check this out too it might interest you ::: Senior Care


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group