Hey,
I'm programming a space combat game, but I'm a bit new to using JMonkey and 3D graphics. What I'm trying to do right now is create a simple dogfighting mechanism for my game. Of course, this has to involve some kind of collision avoidance - the ship will fly towards the target, fire when in range, and turn away before it collides with the target.
Is there any straightforward way to do this? I had an idea to do it with ray picking, but I came across two issues:
1. If I cast a ray from the centre point of the ship in its direction and it hits nothing, that doesn't necessarily mean that it isn't on a collision course - if my ship is huge, it can easily hit something that the ray doesn't.
2. Once I know I'm on a collision course, how would I calculate a "safe" direction as a target to turn towards? I had an idea of a method, but it seems a bit brute force - keep casting rays in a spiral outwards from the facing direction until one misses, then pick that as the target direction to turn towards. The two vectors can then be used to calculate the minimum distance/turning time from the turning speed of the ship. This, however, is still subject to the first limitation.
As I'm working with small objects (i.e. space fighters) for now, I might go with these solutions with pre-set safety margins or something if I can't come up with anything better. Does anyone have any ideas on how I could do this better, though? Any advice would be appreciated.
Thanks,
-Joe
To solve 1, cast a "swept sphere" surrounding the ship hull, instead of a ray.
To solve 2, it depends on what the collider is. You can pre-calculate good places to go for different parts of geometry for different incoming collision courses, and then retrieve that when you detect a collision. Then you don't need to spiral outwards, you just need to turn as hard as you can in the direction indicated until your forward probe doesn't detect a collision.
Another option is to cast swept spheres out 45 degrees in a few directions (4 or 6) and see which one gives you the longest distance to a collision, and pick that one. If one doesn't give any collision at all, that's preferred. Keep turning in that direction until you don't get a collision when moving forward.
Thanks for this - I had a feeling there would be something like that in here. However, I'm not quite sure how to create the bounding spheres - all my ships have bounding capsules for collisions. As far as I can tell, I can make one from the points I get from the geometry, but as I have little idea what I'm doing the amount of code I had to write worries me - this is what I could come up with, which I put in the constructor for my Ship class:
Geometry geom = (Geometry)shipModel;
ArrayList<FloatBuffer> allBuffers = new ArrayList<FloatBuffer>();
int size = 0;
for (int i=0; i<geom.getBatchCount(); i++)
{
FloatBuffer fb = geom.getVertexBuffer(i);
size += fb.capacity();
allBuffers.add(fb);
}
FloatBuffer allPoints = FloatBuffer.allocate(size);
for (FloatBuffer fb : allBuffers)
{
allPoints.put(fb);
}
sphereBounds = new BoundingSphere();
sphereBounds.computeFromPoints(allPoints);
Does this look appropriate? Even if it is, isn't there a simpler way to do it that I'm missing?
Thanks
i use antigravity for collision avoidance. add all antigrav-vectors from everything you want to avoid and fly in that direction. in open spaces -> very effective.
Cleaver monkey
basixs said:
Cleaver monkey :D
You probably meant: Clever hamster! ;)
i still have to implement a 4d-a* to make the ai avoid lots of predictably moving objects…
You probably meant: Clever hamster!
You seem to always know what I mean, rather than what I say :D
HamsterofDeath said:
i still have to implement a 4d-a* to make the ai avoid lots of predictably moving objects...
That sounds mighty impressive, I assume you are just talking about simple platforms and the like, right? You don't mean really things like monsters, and such. :-o
all sorts of projectiles.
So you are teaching them to dodge bullets? Pretty cool
they already do. finally my robocode experience pays off until now, it's simply antigrav-movement which works fine for open spaces and a few little objects, but you can still corner the ai with many big projectiles.