I have a gun shooting bullets in my game and want to detect once a bullet hits a static object like a platform or a wall (to remove the bullet).
The walls are static objects while the bullets are “kinematic”. Since I don’t want the bullets to be affected by gravity, I think there is no other choice as to set them to “kinematic”. The bullets should fly straight into one direction (no gravity etc.)
Evank wrote that “The general rule in Box2D is that a collision event requires at least one of the bodies involved to be dynamic. Static and kinematic bodies don’t collide with each other in any combination.”
Indeed I can’t get the collisions to work. So how could this be solved then…? [import]uid: 9644 topic_id: 6010 reply_id: 306010[/import]
Well, this is what I came up with, too -but what if you need to use gravity in your game? Isn’t there another solution to detect collisions between static and kinematic objects?
[import]uid: 9644 topic_id: 6010 reply_id: 22656[/import]
I’ve never tried it, but an idea that comes to mind is applying a force equal but opposite to the gravitational force to your bullet. The net effect should be that there is no gravity on that object then. [import]uid: 6795 topic_id: 6010 reply_id: 23412[/import]
No, this unfortunately doesn’t work. This will make the object “float” down a little bit first, then it changes direction and lifts slowly upwards like a balloon. [import]uid: 9644 topic_id: 6010 reply_id: 23423[/import]
How about attaching a dynamic sensor to the kinematic bullet?
Another option is to do it without physics.
Not sure if collision events fire without a physics body, but you CAN do distance check (pythagorean theorem stuff) and AABB check (Axis-aligned bounding box) to see if another object’s bounds fall within overlap of the bullet (and do that every frame) But since physics bodies have iterated collision checks between frames, they’re more reliable for collisions than almost any other method I mentioned in the case of very fast moving, small objects like bullets (be sure to set isBullet=true), so although to me it seems rather overkill, you can try attaching a dynamic sensor to the kinematic bullet so the bullet determines the sensor’s position. [import]uid: 63787 topic_id: 6010 reply_id: 84408[/import]
In a much older thread related to the odd effects static objects have on dynamic objects (I’ve lost the link) I think evank pointed out that it was because static objects are effectively “an immovable object” which screws up the physics engine’s math.
I believe the solution to that problem is the same for yours: create a small static object somewhere off screen. Convert any previously static object to dynamic and weld them to the new, hidden static object with a weld joint.
This will cause the objects which need to remain still to do just that but also interact with other sensible objects in mathematically sensible ways, ie: you kinematic objects etc. [import]uid: 8271 topic_id: 6010 reply_id: 84609[/import]