Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (781)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  libGDX apply linear velocity on a body  (Read 581 times)
0 Members and 1 Guest are viewing this topic.
Offline ral0r2
« Posted 2019-01-27 14:38:17 »

Hey guys, i've started playing a little bit around with libGDX and box2d. It's pretty straight forward and very nice but somehow I cant manage to move a body with WASD. I googled a lot but could not finde a solution for my problem.

This is my method for handlingInput:

 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
float velX = 0, velY = 0;
        if(Gdx.input.isKeyPressed(Input.Keys.W)) {
            velY = 200.0f ;
        } else if(Gdx.input.isKeyPressed(Input.Keys.D)) {
            velX = 200.0f;
        } else if(Gdx.input.isKeyPressed(Input.Keys.S)) {
            velY = -200.0f;
        } else if(Gdx.input.isKeyPressed(Input.Keys.A)) {
            velX = -200.0f;
        }
        else {
            velX = 0;
            velY = 0;
        }

        mPlayer.setLinearVelocity(velX, velY);


The position gets updated correctly if I check it with a sysout. My problem is that the box2ddebugrenderer which I use does not update the body accordingly. This might also be a camera problem I assume.

These are my draw and update methods which I call inside of render.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
    /**
     * Used only for graphic to draw stuff
     */

    private void draw() {
        mBatch.setProjectionMatrix(mCamera.combined);
        mB2dr.render(mWorld, mCamera.combined);
    }

    /**
     * Main update method used for logic
     * @param delta delta time received from {@link PlayScreen#render(float)} method
     */

    private void update(final float delta) {
        mCamera.position.set(mPlayer.getPosition(), 0);
        mCamera.update();
        mWorld.step(delta, 6, 2);
    }


And last but not least the constructor for my screen:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public PlayScreen() {
        mBatch = new SpriteBatch();
        mWorld = new World(new Vector2(0,0), true);
        mB2dr = new Box2DDebugRenderer();
        mCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        mCamera.zoom = 10f;
        mViewport = new FitViewport(Gdx.graphics.getWidth() / 50, Gdx.graphics.getHeight() / 50, mCamera);
        mPlayer = ShapeFactory.createRectangle(new Vector2(0,0), new Vector2(32,32), BodyType.DynamicBody, mWorld, 0.0f);
    }


As I said the position gets updated correctly on pressing a button. But the box2renderer does not display the new position of the square. I couldnt find anything on the web regarding this.
If I press "W" for example the rectangle moves up (teleporting - only once) and when I release the button the rectangle gets teleported back to its starting position. The position is increased correctly though. Similiar behavior for the other buttons. I assume that somehow my camera setup is flawed  Huh

Thanks in advance!

PS. I also call

1  
2  
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
inside of my render method.
Offline SugarBlood

Senior Devvie


Medals: 7
Projects: 4



« Reply #1 - Posted 2019-01-27 15:18:05 »

Hi, ral0r2, I assume you using your handlingInput in update method, so shouldn't velX and velY to be variables of class where this method is.

I think isKeyPressed is not for the discrete input so, you don't need this

1  
2  
3  
4  
else {
            velX = 0;
            velY = 0;
        }


MB you better do it like this

1  
2  
3  
4  
5  
6  
7  
8  
9  
if(Gdx.input.isKeyPressed(Input.Keys.A)
  mPlayer.setLinearVelocity(-200, 0);
if(Gdx.input.isKeyPressed(Input.Keys.D)
  mPlayer.setLinearVelocity(200, 0);
if(Gdx.input.isKeyPressed(Input.Keys.W)
  mPlayer.setLinearVelocity(0, 200);
if(Gdx.input.isKeyPressed(Input.Keys.S)
  mPlayer.setLinearVelocity(0, -200);
 


I have to say i'm not familiar with Box2D Grin Tongue but it looks like you better try it, am I right?
Offline ral0r2
« Reply #2 - Posted 2019-01-27 16:12:35 »

Thanks! I agree with you Smiley However this does not solve my problem and is rather a clean code advice  Pointing
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SugarBlood

Senior Devvie


Medals: 7
Projects: 4



« Reply #3 - Posted 2019-01-27 16:33:07 »

oh, I thought its an input problem  Stare

But can I ask one thing

1  
2  
3  
4  
5  
private void update(final float delta) {
        mCamera.position.set(mPlayer.getPosition(), 0);
        mCamera.update();
        mWorld.step(delta, 6, 2);
    }


Why delta here is final?
Offline Gjallar

JGO Knight


Medals: 45
Projects: 1


Follower of Nurgle


« Reply #4 - Posted 2019-01-27 21:26:13 »

The camera is following your player, so of course the player will always remain at the center of the screen. If you add another shape to your game that remains still, you will see that your player moves relative to that shape.
Offline ral0r2
« Reply #5 - Posted 2019-01-27 21:40:36 »

I'm dumb lol. I assumed that it would be visible somehow tho that the shape moves. That fixed it thanks!

@SugarBlood

I used a tutorial but I think it's final because the parameter value of delta is not supposed to be altered inside of the method.
Offline SugarBlood

Senior Devvie


Medals: 7
Projects: 4



« Reply #6 - Posted 2019-01-28 14:46:46 »

Quote from: Gjallar
The camera is following your player, so of course the player will always remain at the center of the screen. If you add another shape to your game that remains still, you will see that your player moves relative to that shape.

ahah, sometimes things like that happens.

Quote from: ral0r2
I used a tutorial but I think it's final because the parameter value of delta is not supposed to be altered inside of the method.

I see, also first time seeing an argument finalized like that Cheesy.
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (1177 views)
2018-11-16 13:46:03

hadezbladez (531 views)
2018-11-16 13:41:33

hadezbladez (1190 views)
2018-11-16 13:35:35

hadezbladez (274 views)
2018-11-16 13:32:03

EgonOlsen (2571 views)
2018-06-10 19:43:48

EgonOlsen (2790 views)
2018-06-10 19:43:44

EgonOlsen (1566 views)
2018-06-10 19:43:20

DesertCoockie (2271 views)
2018-05-13 18:23:11

nelsongames (2123 views)
2018-04-24 18:15:36

nelsongames (2817 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!