Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  [LWJGL] Box2D Ground Collision not working anymore?  (Read 557 times)
0 Members and 1 Guest are viewing this topic.
Offline GamerIDGoesHere

Junior Member





« Posted 2014-03-01 22:34:10 »

Hi,

I am learning JBox2D using Oskar's tutorials (http://www.youtube.com/watch?v=ZKJC2cloIqc),

and I've come across an issue (or maybe I'm a bit thick and I've missed something obvious)

In the tutorial Oskar sets the code up like this:

1  
glOrtho(0, 320, 0, 240, 1, -1);
but I need to use it like this
1  
glOrtho(0, 320, 240, 0, 1, -1);


So I've managed to flip everything up the right way I believe, except for the ground. It doesnt seem to render in the right place or the box doesnt collide with it.

Here is the render code for the ground:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
        BodyDef groundDef = new BodyDef();
        groundDef.position.set(0, height);
        groundDef.type = BodyType.STATIC;
        PolygonShape groundShape = new PolygonShape();
        groundShape.setAsBox(1000, 0);
        Body ground = world.createBody(groundDef);
        FixtureDef groundFixture = new FixtureDef();
        groundFixture.density = 1;
        groundFixture.restitution = 0.3f;
        groundFixture.shape = groundShape;
        ground.createFixture(groundFixture);
        bodies.add(ground);


and here is a link to the rest of the code if you want:
http://pastebin.java-gaming.org/e8f9408558e

Thanks,
- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-03-02 00:39:39 »

Well first let's get the obvious out of the way. What is "height" (the variable you pass into the groundDef)?

Oh wait, I just dug into the class and found height is set to 480. Now, I am assuming you want to ground to render on the "bottom" of the screen. First off, why is your glOrtho call setting the matrix up to only take up around half the screen? I don't see why you do that. Change glOrtho to this:
1  
glOrtho(0, width, height, 0, 1, -1);


Second, (if I remember this correctly), you are setting up the origin to be in the bottom left hand corner. I think. Your height variable is set to 480, so of course it will render at the top of the screen. Change that to be 0. Again, I can't remember if you are using a bottom or top origin, so I might be completely wrong!

Also note I've never used Box2D, so I can't help with your collision code.

Offline SHC
« Reply #2 - Posted 2014-03-02 02:48:18 »

This is the original specification of
glOrtho
function.

1  
2  
3  
void glOrtho(GLdouble  left,     GLdouble  right,
             GLdouble  bottom,   GLdouble  top,
             GLdouble  nearVal,  GLdouble  farVal)

The LWJGL's specification will be like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
/**
 * glOrtho describes a transformation that produces a parallel projection.
 * The current matrix (see glMatrixMode) is multiplied by this matrix
 * and the result replaces the current matrix
 *
 * @param left    Left most coordinate of the window
 * @param right   Right most coordinate of the window
 * @param bottom  Bottom most coordinate of the window
 * @param top     Top most coordinate of the window
 * @param nearVal The near clipping plane
 * @param farVal  The far clipping plane
 */

void glOrtho(double left,    double right,
             double bottom,  double top,
             double nearVal, double farVal)

The documentation is added by me from here.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GamerIDGoesHere

Junior Member





« Reply #3 - Posted 2014-03-02 03:30:02 »

This is the original specification of
glOrtho
function.

1  
2  
3  
void glOrtho(GLdouble  left,     GLdouble  right,
             GLdouble  bottom,   GLdouble  top,
             GLdouble  nearVal,  GLdouble  farVal)

The LWJGL's specification will be like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
/**
 * glOrtho describes a transformation that produces a parallel projection.
 * The current matrix (see glMatrixMode) is multiplied by this matrix
 * and the result replaces the current matrix
 *
 * @param left    Left most coordinate of the window
 * @param right   Right most coordinate of the window
 * @param bottom  Bottom most coordinate of the window
 * @param top     Top most coordinate of the window
 * @param nearVal The near clipping plane
 * @param farVal  The far clipping plane
 */

void glOrtho(double left, double right,
             double bottom, double top,
             double nearVal, double farVal)

The documentation is added by me from here.

Great, but when I fix it so that it follows that pattern
1  
glOrtho(0, width, height, 0, 1, -1);


The box falls up on to the platform at the top, so I change the gravity so it falls down, and I tried to move the platform to the bottom, but that didnt work? The box just falls down.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
        BodyDef groundDef = new BodyDef();
        groundDef.position.set(0, height);
        groundDef.type = BodyType.STATIC;
        PolygonShape groundShape = new PolygonShape();
        groundShape.setAsBox(1000, 0);
        Body ground = world.createBody(groundDef);
        FixtureDef groundFixture = new FixtureDef();
        groundFixture.density = 1;
        groundFixture.restitution = 0.3f;
        groundFixture.shape = groundShape;
        ground.createFixture(groundFixture);
        bodies.add(ground);


Ideas?
Thanks for the Ortho documentation Smiley,
- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2014-03-02 03:37:46 »

The code you just posted is for the ground correct? Not for the box? We need the code for the platform and the box then if you want help!

Offline GamerIDGoesHere

Junior Member





« Reply #5 - Posted 2014-03-02 03:38:44 »

The code you just posted is for the ground correct? Not for the box? We need the code for the platform and the box then if you want help!

Well using the ground as a platform was working, and the rest of the code is in the first post

- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2014-03-02 03:46:29 »

I don't entirely understand your issue then, do you not want the box to move? What happens that you don't want to happen?

Offline GamerIDGoesHere

Junior Member





« Reply #7 - Posted 2014-03-02 03:50:02 »

I don't entirely understand your issue then, do you not want the box to move? What happens that you don't want to happen?

The issue is, when the logic is inverted, and the ceiling is the floor, the box lands on the ground fine and you can move it around,

But when I fix the logic so that the ceiling is 0 as opposed to the window height, and I make the box fall down, the ground acts as if it isnt there.

Sorry if that doesnt make too much sense, I'm trying to explain it in the best way possible

- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #8 - Posted 2014-03-02 04:00:13 »

1  
groundDef.position.set(0, height);

This is the line of code that stands out to me the most. Again, I've never used box2D, so I'm just reading documentation trying to figure this out, so I may be wrong. But, if your origin is at the bottom left hand corner, then setting the position to the height of the window wouldn't work, you need to set the position to be 0, or wherever your ground body is.

Offline GamerIDGoesHere

Junior Member





« Reply #9 - Posted 2014-03-02 04:13:10 »

Well if I make it 0, 0 then it places on the ceiling :/

How should I get it on the bottom?

- Dan
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #10 - Posted 2014-03-02 04:16:07 »

Ah, never mind your origin is in the top left. Hmm... ok I'll take a look over your code again and edit this reply if I come up with something.

Offline SHC
« Reply #11 - Posted 2014-03-02 04:18:13 »

Think it depends on your coordinate system. If your ortho call is

1  
glOrtho(0, width, height, 0, 1, -1);

it sets the coordinate system in a way that (0,0) will be top-left and (width,height) will be bottom-right. So

1  
groundDef.position.set(0, height);

should move the ground to the bottom. You can set the width of the ground to the width of the window and this fixes the problem, I think. No JBox2D experience for me too.

Offline GamerIDGoesHere

Junior Member





« Reply #12 - Posted 2014-03-02 04:20:12 »

Think it depends on your coordinate system. If your ortho call is

1  
glOrtho(0, width, height, 0, 1, -1);

it sets the coordinate system in a way that (0,0) will be top-left and (width,height) will be bottom-right. So

1  
groundDef.position.set(0, height);

should move the ground to the bottom. You can set the width of the ground to the width of the window and this fixes the problem, I think. No JBox2D experience for me too.

Thats what I did, which is why I'm trying to figure out why it doesnt work.

Thanks,
- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #13 - Posted 2014-03-02 04:21:22 »

1  
groundShape.setAsBox(1000, 0);

Maybe this is it? From the docs:
1  
2  
3  
4  
5  
6  
public void setAsBox(float hx,
            float hy)
Build vertices to represent an axis-aligned box.
Parameters:
hx - the half-width.
hy - the half-height.

So, logically you should set the height to at least be 1 to have a height "thickness". I imagine this should do it, this seems to be how Box2D creates it's collision meshes.

Set the box position back to 0, height. You were correct with that. But definitely change the setAsBox function, that should do it!

Offline GamerIDGoesHere

Junior Member





« Reply #14 - Posted 2014-03-02 04:23:30 »

Well it works if I make the position 0, 0 and keep the size as 1000, 0 but not if I make the position 0, height

Thanks,
- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #15 - Posted 2014-03-02 04:25:49 »

I still think you need to change that method. Having 0 for height is telling Box2D you don't want the object to collide on the y axis. Try specifying a negative y variable maybe? Can you post the code for when you change the setAsBox function?

Offline GamerIDGoesHere

Junior Member





« Reply #16 - Posted 2014-03-02 04:31:18 »

Nope both
1  
groundShape.setAsBox(1000, 1);
or this
1  
groundShape.setAsBox(1000, -1);
dont work Sad

Thanks,
- Dan
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #17 - Posted 2014-03-02 04:37:02 »

OK I just read that Box2D won't detect collisions if you don't apply gravity to your scene, which makes sense because why would you waste processor time calculating collisions when the meshes "shouldn't" move? Do you apply gravity to your scene at all?

Edit, never mind you do... I'll keep looking.

Offline SHC
« Reply #18 - Posted 2014-03-02 04:40:15 »

I'm not sure, but I think giving it a try is worth it. Maybe this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
BodyDef groundDef = new BodyDef();
groundDef.position.set(0, height); // Here
groundDef.type = BodyType.STATIC;
PolygonShape groundShape = new PolygonShape();
groundShape.setAsBox(1000, height); // And Here
Body ground = world.createBody(groundDef);
FixtureDef groundFixture = new FixtureDef();
groundFixture.density = 1;
groundFixture.restitution = 0.3f;
groundFixture.shape = groundShape;
ground.createFixture(groundFixture);

Should fix it. Just changed the coordinates to move to top-left as origin from his source code. Say if it works.

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #19 - Posted 2014-03-02 04:42:56 »

But wouldn't that just set the ground to have a collision box in the y axis equal to the entire height of the window?

Offline GamerIDGoesHere

Junior Member





« Reply #20 - Posted 2014-03-02 04:44:34 »

But wouldn't that just set the ground to have a collision box in the y axis equal to the entire height of the window?

Yeah it does Sad
Offline SHC
« Reply #21 - Posted 2014-03-02 04:46:12 »

He used the bottom-left as the origin in his code, so (0,0) will be bottom-left in his case. I've changed it to (0, height) so it will reference bottom-left when top-left is the origin. The same way, I've modified (1000,0) to (1000,height).

Sorry, but can't guess why it didn't work.

Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!