Tag Info

Hot answers tagged

5

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.


5

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

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

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){ ...


4

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 ...


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

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

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 ...


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

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

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 ...


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.


1

It turned out that the vertices of my bullets were not making proper shapes due to a simple mistake. They were something like: (0.0, 0.0),(0.0,0.5),(0.5,0.5),(0.0,0.5) which is obviously wrong but very hard to spot. Thank you for the help everyone, and I did end up using LibGDX; it's awesome.


1

Your problem is that you are trying to create a PolygonShape with more than 8 vertices. Box2D, and thus JBox2D, default to a maximum of 8 vertices per polygon. This is why you don't get an ArrayIndexOutOfBoundsException until index 8 (0 based indexing means it's vertex #9). You can change the maximum number of vertices by simply modifying ...



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