I have a class called Sprite.
I have a class called Bouncing_Ball that inherits from Sprite.
Bouncing_Ball contains a override void called Update() that overrides Sprite's Update() void.
In Game1 I have a list called Sprites. All the Bouncing_Ball instances are there.
In Game1, in the Update() void, for each sprite in Sprites the Update() void of each sprite is called.
Bouncing_Ball's Update() void contains a for each that searches through the sprites to check collisions.
Now the problem is, that with 100 bouncing balls, there is insane lag.
What's the best way to make something like this?
Check if the ball is in the other ones bounding box. If it is do a accurate collisions, if it isn't don't bother.
When you say "void" you mean "function" or "method". void is just the return type in those function signatures, meaning there is no return value.
Post your collision-checking code.
Iterating through all sprites every frame to check for collisions? ouch
Make each sprite send a callback when it collides with another sprite. That way you don't have to iterate constantly.
If they're round, first use rectangular collision detection (with their bounding boxes) then check each individual collision with more precision.
Well, if they're a perfect circle then collision-detection is just a matter of calculating the distance of their centers and comparing with the sum of their radii.
[QUOTE=cracka;17048533]Iterating through all sprites every frame to check for collisions? ouch
Make each sprite send a callback when it collides with another sprite. That way you don't have to iterate constantly.
If they're round, first use rectangular collision detection (with their bounding boxes) then check each individual collision with more precision.[/QUOTE]
That makes no sense, callbacks don't save you from checking for a collision.
[QUOTE=cracka;17048533]Iterating through all sprites every frame to check for collisions? ouch
Make each sprite send a callback when it collides with another sprite. That way you don't have to iterate constantly.
If they're round, first use rectangular collision detection (with their bounding boxes) then check each individual collision with more precision.[/QUOTE]
I don't even know where to start with this post.
[QUOTE=cracka;17048533]Iterating through all sprites every frame to check for collisions? ouch
Make each sprite send a callback when it collides with another sprite. That way you don't have to iterate constantly.
If they're round, first use rectangular collision detection (with their bounding boxes) then check each individual collision with more precision.[/QUOTE]
What calls the callback?
/me waits for the random speech of some 'setCallback' or something and how it magically works.
[code]class Ball : Sprite
{
public override void Update()
{
foreach (Sprite sprite in Game1.Sprites)
{
check collision here
}
}
}
[/code]
And I have multiple Ball instances.
You know that you are checking some sprites multiple times?
You check for collisions n*(n-1) times, although you only need it (n-1)! times.
Do a foreach once and then call a onCollide-function on the colliding Ball instances and then call the Update-function (which doesn't check for collision).
If it is still slow, post your collision detecting algorithm.
[QUOTE=ZeekyHBomb;17063122]You know that you are checking some sprites multiple times?
You check for collisions n*(n-1) times, although you only need it (n-1)! times.
Do a foreach once and then call a onCollide-function on the colliding Ball instances and then call the Update-function (which doesn't check for collision).
If it is still slow, post your collision detecting algorithm.[/QUOTE]
Sorry, I'm a bit confused. Could you post here a code example?
[QUOTE=ZeekyHBomb;17063122](n-1)![/QUOTE]
Are you sure you got that right?
Yes, it's (n-1)!.
It'd be like
[cpp]void MyBallsstoringClass::CheckCollisions(void)
{
Ball a, b;
for(int i = 0; i < NumBalls; ++i)
for(int j = i+1; j < NumBalls; ++j)
{
a = Ballscontainer[i];
b = Ballscontainer[j];
if(CheckCollision(a, b))
{
a.OnCollide(b);
b.OnCollide(a);
}
}
}[/cpp]
That would be quite C++-ish, except that I'd use Ball-pointers. I didn't use pointers because I think they are not exposed in C# (or kinda hidden or something), like in Java.
[QUOTE=ZeekyHBomb;17082067]Yes, it's (n-1)!.
It'd be like
[cpp]void MyBallsstoringClass::CheckCollisions(void)
{
Ball a, b;
for(int i = 0; i < NumBalls; ++i)
for(int j = i+1; j < NumBalls; ++j)
{
a = Ballscontainer[i];
b = Ballscontainer[j];
if(CheckCollision(a, b))
{
a.OnCollide(b);
b.OnCollide(a);
}
}
}[/cpp]
That would be quite C++-ish, except that I'd use Ball-pointers. I didn't use pointers because I think they are not exposed in C# (or kinda hidden or something), like in Java.[/QUOTE]
And you would probably rather like to assign to 'a' in the outer loop rather than assigning to it NumBalls-(i+1) times for no reason.
To NumBalls[i], not i+1; but yes, the thought very true.
[QUOTE=ZeekyHBomb;17082067]Yes, it's (n-1)!.
It'd be like
[cpp]void MyBallsstoringClass::CheckCollisions(void)
{
Ball a, b;
for(int i = 0; i < NumBalls; ++i)
for(int j = i+1; j < NumBalls; ++j)
{
a = Ballscontainer[i];
b = Ballscontainer[j];
if(CheckCollision(a, b))
{
a.OnCollide(b);
b.OnCollide(a);
}
}
}[/cpp]
That would be quite C++-ish, except that I'd use Ball-pointers. I didn't use pointers because I think they are not exposed in C# (or kinda hidden or something), like in Java.[/QUOTE]
If n (or NumBalls) is 10, (n-1)! is 362,880.
In your code the number of comparisons would be 45, I think you're way out.
Uhh... I've mistaken ! then.. what's the function that adds all smaller or equal natural numbers?
n(n+1)/2
Oh yeah, I remember... that formula by Gauß.
I'll just bang my head against the wall for a minute :/
Don't worry about it, Gauss figured it out at eight years old :wink:
[QUOTE=ZeekyHBomb;17082067]That would be quite C++-ish, except that I'd use Ball-pointers. I didn't use pointers because I think they are not exposed in C# (or kinda hidden or something), like in Java.[/QUOTE]
C# uses managed references over pointers. All objects are already managed by reference within C#, there is no special pointer syntax required, the code you posted would already be using pointers for Ball assuming ball is a class, as classes are managed by reference.
[QUOTE=ZeekyHBomb;17083975]To NumBalls[i], not i+1; but yes, the thought very true.[/QUOTE]
No, I was saying how many times you were assigning [b]to it[/b] for no reason. The inner loop always iterates NumBalls-(i+1) times. Okay well minus one from that, because the first time is necessary.
Sorry, you need to Log In to post a reply to this thread.