Jump to content
  • Advertisement
Sign in to follow this  

Flock of sprites, collision detection

This topic is 3017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

For my new game I've been doing a few tests where a squadron of spaceships are following your mouse cursor around, and I'm using some very basic collision detection for making sure the individual ships don't overlap.

Here's the test:http://www.phidinh.com/4xspace/stars/BattleTest.html

The problem I'm having is that the ships are very jerky, at worst they become stuck, which isn't really surprising considering what I've done. I can't turn off the collision detection as then the ships will eventually converge into one point. I'm not sure how to go about tackling this, ideally I'd want the ships movement to be smooth and constantly flowing without getting stuck, so any suggestions would be appreciated. Thanks!

For each ship on each frame, the following code does the work:

// follow target, slows down on approach. 70 is a constant, the lower the faster it travels
var diffX:Number = (targetPoint.x-this.x)/70;
var diffY:Number = (targetPoint.y-this.y)/70;
this.x += diffX;
this.y += diffY;

// simple collision detection, cancel move if collided
for each(var ship:Ship in ArenaModel.allShips){
if(this!=ship && this.hitTestObject(ship)){
this.x -= diffX;
this.y -= diffY;

Share this post

Link to post
Share on other sites
There are many solutions to this problem. One of them is not canceling movement. Why don't you just extrude them if they overlap? I'm not sure how your collision detection works, but it's a very easy task. Pseudocode would be:

//Move ships towards the point
moveShips(var targetPoint);
//If a given pair collides, separate them using your separation algorithm
for(int i=0;i<ships.size;i++)
for(int j=i+1;i<ships.size;j++)
if(hits(ship, ship[j]))
separate(ship, ship[j]);

I can't give you more precise code for separation since I don't know what shapes are your ships treated as. However, the approach is the same:

1. Check if they collide
2. If they do, calculate the depth of their overlapping along the line joining their centers.
3. Separate them by adding vectors (to their positions) that sum up to the overlapping depth. Here you can do it your own way - you can just move them equally, you can try to determine who rear-ended whom and separate the one that did, the choice is entirely yours.

Share this post

Link to post
Share on other sites
Thanks Ratslayer,
I tried to implement your solution and it works a lot better, but performance was really bad especially looping through 100+ ships and nested loops to detect collision.

In the end I used Box2D physics instead to model the collisions, that achieved the desired behaviour I wanted without too much work!


Thanks again for the ideas

Share this post

Link to post
Share on other sites
It looks good. I don't know whether it will be a problem in your game, but you may want to disable seeking under a certain minimum distance threshold. That prevents a ship that actually reaches the target point from 'freaking out' as it sometimes does in your example.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!