I have a Spaceship which can fire off bullets at any angle. When the bullets hit the walls they are removed and a sound effect is played. This is all good, but I have a problem in that when the bullets hit the enemy-spaceship - they push it around the screen (a lot) which I don't want to happen. I have purposefully made the bullets heavy so that they can push other objects around.
I decided to make an extra sensor shape - exactly the same shape as the bullet, and then filter the main bullet shape so it would not hit the ship.
The first thing I did was to totally replace the shape with a sensor - just to check if it would work. It did but I noticed that it was passing much further into the walls before it was removed. Does this mean that sensors aren't really suitable as bullets? If so what should I do?
I'd appreciate any help. Thank you very much.
It's passing further as it is not stopped from passing at all. That's fairly harmless.
It is possible for the shape to be moving so fast that they go straight through the wall, called tunnelling. But ordinary shapes would do this just as much, and it sounds like you aren't having that problem. If so, you need to set up the bodies as Bullets (meaning the Box2D technical term, not just ammunition in general like you meant it).
I see what you mean as to why they appear to pass into the wall. Thanks. However is there any way I can have a bullet that is a sensor that won't pass into a wall/object? Since it doesn't look very good in the game 🙂
Or is there another way I can have a bullet which has zero effect on the object it hits? - without changing its density. The only way I can think of (to solve the problem with my game) is to effectively create 2 bodies for every bullet - one a sensor and one a normal heavy bullet - where destruction of one means the destruction of the other.
Thanks
Mmph, not sure. The only "legal" way I can think of would be two bodies, a very light one and a very heavy one filtered appropriately, and using the current position and velocity from the most recently collided. Could behave oddly in some circumstances.
But you might get better effects from just having one body, and changing the mass of it during the Add event. Not sure if that would work though, and would take some hackery to stop Box2D asserting against it.
For a massless bullet you should use a ray cast.
How do I simulate the delay (the time it would normally take for the bullet to reach the player) with a RayCast? I suppose I could do a delay proportional to the distance involved - is the right way?
Actually I'm not sure if the sensor is working properly as a bullet (SetIsBullet(True)) - My spaceship is made up of lots of pieces - so I can make bits fall off it as it gets damaged. and - the bits on the far side are falling off because the bullet is passing through the ship, and missing those at the front. Does this mean that a sensor cannot be set as bullet? I am detecting on add and persist contact points.
I think I have the idea - I still have the bullets but I do raycasts from the end of each bullet, and then destroy them when the distance is zero or very small. Then I could have the bullets as sensors.
I did think of another way round my particular problem - where I would like some objects to be pushed by the bullets whereas some don't get pushed but still register the hit. I thought I could make the bullets with a very small mass so they don't actually push any objects - but so they are still detected properly (unlike sensors) - then when they hit the objects that can be pushed - I apply a force/impulse manually to the object at the point of impact in the same direction of the bullet. I don't know how this would appear in practice but it sounds ok on paper. What do people think about this idea? Thanks.
I have had problems with this in two projects. Raycasts are simply not always gonna do the trick. There must be a way to make the collision reaction go into one body only.
Hello Erin!
It sounds like that would do the trick.
Basically we need to have bodies that collide, bounce, react etc. with everything like it normally should, but it should never influence any other objects.
We are using this for physical interface objects. Yes it sounds weird, but you can also use this for arrows, bullets, a football etc.
For bullets someone could of course use ray tracing, but for for example arrows it is a pain because they do not have a straight flightpath. It would be nice to use normal bodies, so you know when and where it hit another body exactly. This is handy for decals etc.
Thanks for the reply Erin.