# Tag Info

7

The first argument of applyLinearImpulse is the vector which is the strength of the force/impulse. So just change it to something else rather than 0,0 and your object should fly.

6

I think you should include food in the simulation. If you do you will already have efficient checks for collisions with player body which you will need to make yourself otherwise. If you do not iclude it in the simulation you might need to implement a sweep and prune algorithm yourself (or maybe some space partition or similar) that box2d will already be ...

5

My thought is that you could use it - Say as a static body and assign it to an impact group that has no effect on other bodies. But like you say, box2d will do collision for you so it would make it a lot easier to implement pacman eating the food. And also like you say, it would be asleep so it wouldn't cost too much more in computation. Why make it more ...

5

This simple algorithm assumes you're working with a convex polygon (in the case of physics engines you definitely should be): Loop over the polygon with two indices, one called next and one called last. This is also known as a format called "Triangle Fan". Here's a demo image: You set up these two indices and loop something like this: TriVertexList ...

4

The most common approach to this is to detect if the character is standing on a solid surface before allowing them to jump. This restricts players to a single jump since the first jump puts them in the air, then they're no longer on a solid surface, so the next time they try to jump, they can't. For Box2D specifically you can see how to implement that here: ...

4

To expand on Sean's comment, you should create a separate class or set of classes to connect your physics world with your rendering. I'm not entirely familiar with Processing, but usually it'd be something like this: class Box2DObject{ final float PHYSICS_SCALE = 100.0; b2Body body; Image image; Box2DObject(b2Body ibody, Image iimage){ ...

3

There's nothing wrong with Box2D web - perhaps just the way you are trying to draw the data. Are you using the debugRenderer? Box2D - A web port Physics simulations are always decoupled from the rendering routines (or they should be!). This is because they typically work within their own spatial regions that calculations are done in. This is metric in the ...

2

First thing I would look at is how setting the center point of object affects the local coordinate space of your polygon vertices. Box2d gives you the position of the geometry at its center of mass by default, and the verts will be offset from the center in local space. I would try commenting out the lines: p.setCenterX(center.x); p.setCenterY(center.y); ...

2

What you have here is a misunderstanding of how physics engines work. When a force is applied, it is merely set as some state indicating that the force was applied. The actual movement does not yet happen. The physics engine needs to integrate the physics with a time step, do collision checks, and then resolve those collisions (which can apply more ...

2

I recently implemented a similar detection in a platformer using Box2D, to know if my player body was touching the ground (to allow the player to jump—or not). There are many ways to do it, but here is my way. Give your character body two fixtures; a solid one and a sensor. (If you're unfamiliar with the concepts, read the relevant manual chapter.) ...

2

If I'm reading correctly, you want the camera to center on the player except when the player is at the edge of the map. Well, to do this you should first get the world coordinates of the screen, and just move the camera to stay centered around the player. However, you will want to clamp the x and y coords of the camera to the boundaries of the map: //calc ...

2

Don't use a variable time step for physics, this will mess up the entire engine. Box2D will attempt to apply enough force depending on the level of penetration, if the step is constantly changing then Box2D is getting different numbers each time it tries to separate fixtures. Use an fixed timestep instead: world.step(1f/60f, 5, 8); Or some other ...

2

It all depends on the complexity of your simulation. The more objects you have the more time it will take to solve the collisions. You can set your food filters to collide only with your player and your food bodies type to static. Then when you collide simply disable the collision resolution and add your food objects to the "end of lived" list. You might ...

2

You're attempting to mix your data object with your visual objects. For the purpose of this issue, this is a big no-no. Your screen is a visual object, and your sprites/models/etc are visual objects. Those will matter. Box2D on the other hand is not a visual object, so there is no magical direct mapping. You could decide that arbitrarily the top-left ...

2

In your situation, having states within states is perfectly acceptable and is often the expected architecture. Here is something to think about: generally, during development, you could start your game directly in the "play" state (you set up a bunch of parameters that would normally be set up by the previous state), and you make the game end instead of it ...

1

I had the solution for awhile, but I forgot to post. Here's the method to move a list of enemies towards the player. public void moveAlien() { for (Aliens a : aliens) { //List of Aliens (Enemy) Sprite s = a.getSprite(); //Get current enemy's sprite float targetX = spacemarine.getX(); //Player's X float targetY = spacemarine.getY(); //Player's Y ...

1

Try using vector math instead of cos/sin angles. Something like this might work for you; public void moveAlien() { float velocity = 50; Vector2 ap = new Vector2(AlienBody.getPosition()); Vector2 mp = new Vector2(Marinebody.getPosition()); Vector2 delta = mp.sub(ap).nor(); // This is not a unit vector pointing in the direction from ap to mp ...

1

I haven't used JBox2D, but I think this is accurate. I looked at the source for the project and learned a lot (and you can too). Primarily that body contains a Transform, and that the Transform contains the position. Since all those variables are public, it's likely you'll be able to access the position from: myStaticBody.m_xf.p

1

For most scenarios it's beneficial to wrap the objects you have no control over (box2d in your case) in your own objects with custom serialize methods. That or build a custom serialize method in your world manager presumably you have a list of the bodies somewhere (for destroying later), which you could flatten down in a serialize method for the whole world ...

1

It works now! I have created a seperate Box2D world for every planet and Solar Systems. The Solar System rotates the planet, so it looks like the planet is rotating. But the player (in the world) isn't rotating at all. The result looks like this: http://imgur.com/rPM3OgV

1

Imagine that you have a class that represents a permeable object (in pseudocode): class permeableObject { b2RigidBody body; //this holds the actual box2D rigid body //this is a virtual function; it will be override by derived classes vec2 getExertedForce(vec2 point); //... //functions such as constructor, setBody, etc } Then, you ...

1

If in your game there are only a couple of objects that could possibly go outside of your game limits, you don't have to add rectangles to set the boundaries in your game unless it somehow makes your code cleaner in your case. Efficiency-wise I guess the old fashioned if (object1.Position.X < 0) qualifies as the left boundary in your game. If you have ...

1

Okay, I found an answer. You need to set the camera's position directly after calling world.step() and then it works without shifting. world.step(...); camera.position.set(...); camera.update(); (Original Answer)

1

I think you are approaching this the wrong way. Box2D is doing exactly what you want it to do, the problem may be with what you want. I assume you are applying gravity? If the force of gravity is less than the upward velocity from your character running against the slope, you are probably applying too much force (or too little gravity) while making your ...

1

Turns out, there was two problems. One was I set the size property of my entity after instantiating but did not update the Body size. The other issue is I didn't realize box2d positions was centered for each body.

1

Take a look at the method you are using on this line: body.createFixture(footSensor); It returns the fixture: public Fixture createFixture (FixtureDef def); So the only thing you need to do is: Fixture footSensorFixture = body.createFixture(footSensor); footSensorFixture.setUserdata("sensor"); Also your naming convention is a bit confusing. You are ...

1

As per Andy's comment: Nevermind, I fixed it. I just had to take out the p.setCenterX(center.x);p.setCenterY(center.y); out of the render function.

1

The "Physics" and p5 package/class was an extension to the jbox2d framework the guy wrote. It seemed pretty nice but not ideal for what I'm doing. I ended up doing this: import org.jbox2d.common.*; import org.jbox2d.dynamics.*; import org.jbox2d.collision.shapes.PolygonShape; import processing.core.PApplet; public class MyJBoxTest extends PApplet { ...

1

You can use GLES_Render class in TestCpp, which is located in ./samples/TestCpp/Classes/Box2DTestBed/GLES-Render.cpp ./samples/TestCpp/Classes/Box2DTestBed/GLES-Render.h of the download package. To implement the debug draw, you can draw the debug renderer with your world data after CLayer::draw(); as demonstrated in samples/TestCpp/Classes/...

1

I don't know about JBox2D but in the actual Box2D lib there is PolygonShape.GetVertex() //Returns a Vec2 (the coordinates) of a vertex given its index const b2Vec2& GetVertex(int32 index) const; So according to your code, I think calling shape.GetVertex(1) returns the second vertex you added.

Only top voted, non community-wiki answers of a minimum length are eligible