Reputation: 286
I have one Sprite and multiple bitmaps that work as bullets. Now the problem is that I want the bullet to go really fast but that again brings a problem to my collision detection function, The way it works is that every frame I create a Rect at both the enemy and the bullet and check for overlaps.
Now if a bullet goes really fast it like "jumps" from point to point and that means if a enemy is small and the bullet "jumped" over the enemy it wouldn't get noticed.
What I wanna know is if there is a way to like detect if a collision is going to happen between 2 moving objects or just look if a enemy is in the trajectory of the bullet.
I made the collision detection by looping through an array of enemies and inside of that for loop I loop all bullets and then create a rect for both and check for collisions with Rect.intersects for each enemy and bullet.
The bullets heading is measured by one fixed point and touch input and then calculated by this function:
public void calcPoint(float x, float y) {
double alfa = Math.atan(x / y);//x and y are inputs
bulletPointX = (float) Math.sin(alfa) * speed;
bulletPointY = (float) Math.cos(alfa) * speed;
}
I have no idea how to fix it and I'd like some adivice how to do it and maby an example...
Upvotes: 3
Views: 454
Reputation: 22342
There are several ways to do this, but the easiest is probably predictive collision detection. Basically, you split the movement into time segments and check for each segment. It may not be very efficient if you have a few thousand objects moving around, but for most cases it'll work just fine.
It's known by a few names, also. The link I gave can help with the basics, but to learn more, do a search for:
Upvotes: 1