I am currently working on a game where the world will have a fixed size say 2000x4000, but there will be no boundaries. When you get to one edge you just wrap around to the other side.

I have visibility of objects working OK. When you can see 0,0 and 2000,4000 at the same time, objects in all the corners draw in their proper relative positions, but I am sort of brute forcing it. I check x,y x+2000,y x+2000,y+4000 and x,y+4000 to see if any of those locations are visible.

Do I need to do something similar for collisions? Right now, if the bounding circle of an object crosses the world boundary, it won't register a collision with an object on the other side, and the two can penetrate and overlap. I am concerned a bit about performance as I am treating each object as if it was in four different places for drawing and that would cause a lot of collision checks. For objects to register a collision they would need to check if any one of their four positions collided with any one of another objects four positions. I might be able to limit that to only doing the extra checks to when the bounding box crosses a boundary.

And that brings me to path finding. For that, I feel I am going to need four positions for every object all the time in order to find the shortest path between two of them for the computer AI to chase and target properly.

I am hoping I am waaay of base and there is a much less computationally intensive way to solve this problem.

What you need to do is to represent your world differently. Right now you're seeing it sort of as an array - when you get to the edge you fall off. What would probably be better for your purposes would be to create a bunch of links. Have each tile have pointers to its north, south, east, and west, connections, then you can just connect the ends to each other. For pathfinding and whatnot, this will work perfectly. Then for drawing, you simply create a recursive function (or something similar), that draws all grid spaces that are an X distance of i away from the player, and a Y distance of j. In this case, the drawing becomes more processor intensive than it was before, but collision and pathfinding are very fast. You could try combining the two by storing every room in an array but also allowing them to link to their neighbors.

You can also simply turn array access into a function. Instead of saying array[m][n], you would say, getArray(m,n), and then make your function like this:

Thanks for the reply, but it looks like I need to be a bit more clear about what I am doing.The world isn't tile based at all, and there are no rooms. Think asteroids where you wrap from one side of the monitor to the other. Except in mine, the world is larger then what is displayed on the monitor.Have a look at this partial screenshot The red line is the world boundary.I can move across the edges just fine. Collisions don't work properly if the bounding circle overlaps the edge. ie The objects don't wrap until their centers cross the edge.All objects have an x,y location (where x and y are doubles) and an associated velocity which has both the angle/magnitude of movement and double x and y components of the movement.At the moment, I just have the list of objects to work with.

Okay, got it. You're right that this way things get a bit more complicated, but not too much so.

All you need to do is represent collision as a total geometry - when the ship moves over an edge, subtract the part off the screen from that edge and move it to the other side, then union the two parts. I'm not sure how you're handling collisions, but you can always use Java's Area class to handle the geometry for you.

Actually, it helps to think about it like the monitor was the entire world. When I am crossing the edge I am on both sides at the same time (or all four if I hit the corner).I think I can get away with doing an extra check (or three) if my bounding circle crosses an edge. Essentially, when it is crossing the edge it is in up to four locations at the same time. I think the easiest way to do that is going to be to modify the GameObject to support the idea of a temporary object that is only allowed to exist as long as it is overlapping the world bounds. This temporary object would have a "parent" object that it is linked to and when the either the parent or the temporary object has a collision, both objects would have their movement vectors updated. It is a big enough world that there shouldn't be more than two or three objects crossing the edge at once, so it shouldn't be a big performance hit.

This is what I am currently doing to test for collisions. If I add the temporary object, I won't have to change this at all.

The problem I was trying to avoid was with the enemies finding their optimum attack vector. It looks like for that, the ship and the enemies will each need four locations all the time and I will need to find the shortest permutation. 4! = 24, but with symmetry I can drop that to 12. 12 distance calculations per enemy, this is probably going to be my performance bottleneck

Yeah, that will be a pretty big hit to performance. Why not improve your actual collision algorithm? You can put everything in "buckets" and then use a sort of quad tree algorithm to speed collision implementation. I can't see a game like that creating much of a collision bottleneck as long as you are using smart algorithms, even if you've got 4 collision tests per object. Simply start with location buckets (like a 10x10 array of lists of objects within those quadrants), then use AABB (Axis Oriented Bounding Box) collision, then use geometry collision. Because you narrow things down along the way, you don't have to do the expensive geometry collision except for very close objects.

I am not sure that the collision algorithm is going to be the issue. I am checking every object against every other object to see if they will move to or past collision in the next time tick, but I have not figured out pixel level collision detection yet, so it is simply bounding circle. I can try the spacial hashing, but I will have to deal with my edge problem in every hash bucket. I will need to add objects that overlap hash edges to all the buckets they overlap. Any recommendations on bucket size or number of buckets? Two few, and I don't get any benefit, too many and I have a lot of overhead checking for non-empty buckets.

I think I may have just thought of a trick I can use to solve the issue of finding the shortest attack vector. If I translate the player's ship to the center of the map and apply the same relative transformation to the enemy ships, then the distance should be accurate.

So, in the example I posted a picture for, let's assume the asteroid is an enemy The ship was at 36, 5936 and the center of the map is at 2400, 3000 so the ship needs to move by 2364, -2936The "enemy" was at 4653, 44 and applying the same transformation puts it at 7017, -2892.Bringing those coordinates back into the world gets me 2217, 3108That's a dx of 183 and a dy of 108 for a distance of 212.492.Yay! it works. So two transformations and two mod operations beats 12 separate distance calculations.

Yeah, you don't really need to translate anything, though, do you? Just use the absolute value.

As for the buckets, probably a 10x10 grouping would be good. You can add the same object to 4 different buckets if you want, it won't be a big deal. Either way, you're only checking a couple buckets at the maximum so you'll ignore the extra objects.

Even so, if you're just doing radial collision than I'm not sure you even have to worry about it all. Doing the absolute worst algorithm with a bunch of wasted comparisons will probably still be plenty fast for a game of this size unless you've got hundreds or thousands of objects on the screen at a time.

Yeah, you don't really need to translate anything, though, do you? Just use the absolute value.

Not sure what you mean here. Using original positions and absolute value I get the following:dx = 4617dy = 5892which is a distance of 7485.4761 and an attack vector going in pretty much the opposite direction of what it should.

As for the buckets, probably a 10x10 grouping would be good. You can add the same object to 4 different buckets if you want, it won't be a big deal. Either way, you're only checking a couple buckets at the maximum so you'll ignore the extra objects.

Even so, if you're just doing radial collision than I'm not sure you even have to worry about it all. Doing the absolute worst algorithm with a bunch of wasted comparisons will probably still be plenty fast for a game of this size unless you've got hundreds or thousands of objects on the screen at a time.

That is what I was thinking as my object count is going to be low, probably less than 100. But I can try it out and see if I get any benefit.

To avoid having to do this 4 times (or to have temporary objects) you just need to normalise the deltas. As long as the sum of the radii of the objects is less than half the dimension length (call this L), then you can just normalise the delta values to be in the range -L/2 ... L/2.

I'm not convinced that the above code will actually work I'm afraid. It's important that it's the DELTAS that are normalised. I.e. the values AFTER the subtraction.

I think you'll find that your code above will now fail if, for example, A.x is just smaller than L/2 and B.x is just greater than L/2. A would end up heading left to get to B, when it should be heading right. You've just moved the problem from the boundaries of the space to the centre.

You are correct in that I now have issues at the center of the world.The values for center.x and center.y will never be negative, so the additional checks you suggest will never happen.And the direction of that vector is important as it is being used to determine if the collision will happen within the given time tick.Maybe I need to work on copies of the objects and translate one of them to world center and apply the same transformation to the other. I don't think actual position will impact the calculations as long as relative positions remain constant.

And the direction of that vector is important as it is being used to determine if the collision will happen within the given time tick.

So long as the subtractions to calculate the deltas (i.e. relative vectors -- both position and velocity) are both done the same way around, the relative signs should be correct. And as long as the delta-velocity isn't too high (i.e. abs(deltaV.x) * t < L/2, etc.) then the collision time calc should work as expected too.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org