Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  [SOLVED?]Changed box2D Example, dont work!  (Read 216 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-06-02 01:04:07 »

Hi Guys, im trying to make the Greenballs move at my Screen, but somehow it isnt showing up. But it compiles just fine.
I changed the example, to just show the balls on the screen.

Just the ground is drawned, the balls arent.

What am i doing wrong ?


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
package grassproject;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;


public class App extends ApplicationAdapter {

    private static final float VIEWPORT_WIDTH = 15;
    private OrthographicCamera camera;
    private float w;
    private float h;
    //
   private World world;
    //Ball
   private Sprite[] ballSprites;
    private Texture ballTexture;
    private int MAX_BALLS;
    private Body[] ballModels;
    private static final float BALL_RADIUS = 0.15f;
    //Ground ::
   private Sprite groundSprite;
    private Texture groundTexture;
    // Render general
   private SpriteBatch batch;

    @Override
    public void create() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        //
       world = new World(new Vector2(0, -10), true);
        batch = new SpriteBatch();

        //
       camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * h / w);
        camera.position.set(0, camera.viewportHeight / 2, 0);
        camera.update();

        createSprites();
        createBalls();
        createGround();

    }

    private void createSprites() {
        ballTexture = new Texture(Gdx.files.internal("imagens/ball.png"));
        ballTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

        ballSprites = new Sprite[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i] = new Sprite(ballTexture);
            ballSprites[i].setSize(BALL_RADIUS * 2, BALL_RADIUS * 2);
            ballSprites[i].setOrigin(BALL_RADIUS, BALL_RADIUS);
        }

        groundTexture = new Texture(Gdx.files.internal("imagens/white.png"));

        groundSprite = new Sprite(groundTexture);
        groundSprite.setSize(VIEWPORT_WIDTH, 1);
        groundSprite.setPosition(-VIEWPORT_WIDTH / 2, 0);
        groundSprite.setColor(Color.GREEN);

    }

    private void createBalls() {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyDef.BodyType.DynamicBody;

        CircleShape shape = new CircleShape();
        shape.setRadius(BALL_RADIUS);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        ballModels = new Body[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballModels[i] = world.createBody(ballBodyDef);
            ballModels[i].createFixture(fd);
           
        }

        shape.dispose();
    }

    private void createGround() {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 0);
        bd.type = BodyDef.BodyType.StaticBody;

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(VIEWPORT_WIDTH, 1);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        world.createBody(bd).createFixture(fd);

        shape.dispose();
    }

    @Override
    public void render() {
        GL10 gl = Gdx.gl10;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        world.step(1 / 60f, 10, 10);

        for (int i = 0; i < MAX_BALLS; i++) {
            Vector2 ballPos = ballModels[i].getPosition();
            ballSprites[i].setPosition(ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2);
            ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees);
        }

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        groundSprite.draw(batch);
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i].draw(batch);
        }
        batch.flush();
        batch.end();


    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}

Offline Andre Lopes
« Reply #1 - Posted 2013-06-02 01:59:40 »

Fixed, max_balls was == 0

now it does draw the ball, BUT, the balls just goes down and up. They collide but they dont move right, left.
Actually, they fall together, like they are glued.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
package grassproject;

import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import java.util.Random;

/**
 *
 * @author André Vinícius Lopes
 */

public class App extends ApplicationAdapter {

    private static final float VIEWPORT_WIDTH = 10;
    private OrthographicCamera camera;
    private float w;
    private float h;
    //
   private World world;
    //Ball
   private Sprite[] ballSprites;
    private Texture ballTexture;
    private int MAX_BALLS = 2;
    private Body[] ballModels;
    private static final float BALL_RADIUS = 0.15f;
    //Ground ::
   private Sprite groundSprite;
    private Texture groundTexture;
    // Render general
   private SpriteBatch batch;
    private final Random rand = new Random();

    @Override
    public void create() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        //
       world = new World(new Vector2(0, -10), true);
        batch = new SpriteBatch();

        //
       camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_WIDTH * h / w);
        camera.position.set(0, camera.viewportHeight / 2, 0);
        camera.update();

        createSprites();
        createBalls();
        createGround();

    }

    private void createSprites() {
        ballTexture = new Texture(Gdx.files.internal("imagens/ball.png"));
        ballTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

        ballSprites = new Sprite[MAX_BALLS];
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i] = new Sprite(ballTexture);
            ballSprites[i].setSize(BALL_RADIUS * 2, BALL_RADIUS * 2);
            ballSprites[i].setOrigin(BALL_RADIUS, BALL_RADIUS);
        }

        groundTexture = new Texture(Gdx.files.internal("imagens/white.png"));

        groundSprite = new Sprite(groundTexture);
        groundSprite.setSize(VIEWPORT_WIDTH, 1);
        groundSprite.setPosition(-VIEWPORT_WIDTH / 2, 0);
        groundSprite.setColor(Color.GREEN);

    }

    private void createBalls() {
        BodyDef ballBodyDef = new BodyDef();
        ballBodyDef.type = BodyDef.BodyType.DynamicBody;
        //ballBodyDef.position.x = w - 20;
       //ballBodyDef.position.y = 15;

        CircleShape shape = new CircleShape();
        shape.setRadius(BALL_RADIUS);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        ballModels = new Body[MAX_BALLS];

        Vector2 vec = new Vector2();

        for (int i = 0; i < MAX_BALLS; i++) {
            ballModels[i] = world.createBody(ballBodyDef);
            ballModels[i].createFixture(fd);
            float tx = rand.nextFloat() * 1.0f - 0.5f;
            float ty = camera.position.y + camera.viewportHeight / 2 + BALL_RADIUS;
            float angle = rand.nextFloat() * MathUtils.PI * 2;
            ballModels[i].setLinearVelocity(vec.set(0, 0));
            ballModels[i].setAngularVelocity(0);
            ballModels[i].setTransform(vec.set(tx, ty), angle);

        }



        shape.dispose();
    }

    private void createGround() {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 0);
        bd.type = BodyDef.BodyType.StaticBody;

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(VIEWPORT_WIDTH, 1);

        FixtureDef fd = new FixtureDef();
        fd.density = 1;
        fd.friction = 0.5f;
        fd.restitution = 0.5f;
        fd.shape = shape;

        world.createBody(bd).createFixture(fd);

        shape.dispose();
    }

    @Override
    public void render() {
        GL10 gl = Gdx.gl10;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        world.step(1 / 60f, 10, 10);

        for (int i = 0; i < MAX_BALLS; i++) {
            Vector2 ballPos = ballModels[i].getPosition();
            ballSprites[i].setPosition(ballPos.x - ballSprites[i].getWidth() / 2, ballPos.y - ballSprites[i].getHeight() / 2);
            ballSprites[i].setRotation(ballModels[i].getAngle() * MathUtils.radiansToDegrees);
            System.out.println(ballPos.x + " ; " + ballPos.y);
        }

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        groundSprite.draw(batch);
        for (int i = 0; i < MAX_BALLS; i++) {
            ballSprites[i].draw(batch);
        }
        batch.flush();
        batch.end();


    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}
Offline Andre Lopes
« Reply #2 - Posted 2013-06-02 03:57:24 »

Fixed..

1  
2  
ballModels[i].setBullet(true);
             ballModels[i].setActive(true);


Hm. Was kinda lucky Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (62 views)
2014-04-15 18:08:23

BurntPizza (60 views)
2014-04-15 03:46:01

UprightPath (73 views)
2014-04-14 17:39:50

UprightPath (56 views)
2014-04-14 17:35:47

Porlus (73 views)
2014-04-14 15:48:38

tom_mai78101 (99 views)
2014-04-10 04:04:31

BurntPizza (159 views)
2014-04-08 23:06:04

tom_mai78101 (254 views)
2014-04-05 13:34:39

trollwarrior1 (208 views)
2014-04-04 12:06:45

CJLetsGame (215 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!