

Libgdx Box2D - Collision detection
source link: https://www.codesd.com/item/libgdx-box2d-collision-detection.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Libgdx Box2D - Collision detection
I am trying detect player collisions with a sensor body. Imagine a collectible in a platformer... I want to run an event when the player collides with the collectible.
As you will see from my code, I am attaching a dynamic body to the collectible object as well as a sensor body which I am hoping to attach a player collision 'event' to when the player collides with the sensor. It seems there is a "ContactListener" interface, but implementing the methods don't seem to do anything. How would I go about doing this?
If there is a better way of doing any of the below, any advice would be appreciated :)
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class CoinCollectible extends GameObject{
public CoinCollectible(Vector2 position, float angle){
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.x = position.x;
bodyDef.position.y = position.y;
bodyDef.angle = angle;
PolygonShape poly = new PolygonShape();
poly.setAsBox(1, 1);
FixtureDef itemFixture = new FixtureDef();
itemFixture.shape = poly;
itemFixture.density = 1;
itemFixture.filter.categoryBits = PhysicsLayers.LAYER_ITEM;
itemFixture.filter.maskBits = PhysicsLayers.MASK_PLAYER;
fixtureArray.add(itemFixture);
massData.mass = 1f;
// Attach a collision sensor
FixtureDef sensorFixture = new FixtureDef();
sensorFixture.shape = poly;
sensorFixture.isSensor = true;
sensorFixture.filter.maskBits = PhysicsLayers.LAYER_PLAYER;
fixtureArray.add(sensorFixture);
// Clean up
poly.dispose();
}
}
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.World;
public class GameObject{
public Body body = null;
public BodyDef bodyDef = new BodyDef();
public MassData massData = new MassData();
protected List<FixtureDef> fixtureArray = new ArrayList<FixtureDef>();
public GameObject(){}
public Body addToWorld(World world){
if(bodyDef == null)
return null;
body = world.createBody(bodyDef);
while(fixtureArray.size() > 0){
body.createFixture(fixtureArray.remove(0));
}
body.setMassData(massData);
return body;
}
}
Try add this line in your code (at coin constructor):
bodyDef.userData = this;
And your contact listener, get userdata
and casting into GameObject
, then, for example, call event collision.
Greetings
Recommend
-
114
Collisions Collisions is a JavaScript library for quickly and accurately detecting collisions between Polygons, Circles, and Points. It combines the efficiency of a
-
107
XNA pixel perfect collision detection with transforms ported to Javascript Posted byu/rscott442...
-
93
A Simple 2D Golang collision detection and resolution library for games - SolarLune/resolv
-
52
This is a series about Star Anise Chronicles: Cheezball Rising , an expansive adventure game about my cat for the Game Boy Color. Follow al...
-
10
服务端Box2D进阶 2017/03/05 · Leave a comment 此文紧接我的上一篇博文 –
-
6
服务器端Box2D入门 2017/02/18 · Leave a comment
-
8
Relative performance for collision detection techniques in ActionScript 3 Friday, June 26, 2009 If you have read my blog any
-
6
Strategies for optimizing collision detection with BitmapData.hitTest Thursday, June 25, 2009 Yesterday I blogged about how you can use the
-
2
MotivationMaking their own tiny video games can be a great way for kids to learn programming in a playful matter. While Jupyter is widely used as a scientific and educational tool,
-
5
cocos2d, Box2D 2015-02-21 cocos2d和cocos2d-x虽然都是一家的产品,但前者是用python写的,而后者是用C++写的。 我试着在windows下安装cocos2d,...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK