Jump to content
  • Advertisement
Sign in to follow this  
Silicon Seed

Tedius physics in my logic code

This topic is 3771 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

Edit: I also just realized that my code isnt right.. I am gonna update it to something that works a liitle better in this post. Hello, I am creating a 2D application with sprite balls that bounce off each other. I want to have alot of them. Currently I am writing the code for their bounce detection like this to start out with: // Detect if ball 0 has collided with ball 1 //see if the ball 0's Y position has entered ball 1's Y position if(ball[0].y + ball[0].h > ball[1].y && ball[0].y < ball[1].y + ball[1].h && ball[0].x + ball[0].w > ball[1].x && ball[0].x < ball[1].x + ball[1].w) { //reverse y direction only (blah boring) ball[0].ymomentum = -ball[0].ymomentum; } Now if I want to have 100 balls I need to write this code 10,000 times..... for each ball to detect each ball. I am having trouble trying to figure out how to write a for statement or something that will take an integer and do this with one statement for as many balls as I want. Also as I change the detection code it would be simpler to do it once then 10,000 times :)like boarder detection needs to be added as well: //detect if Ball 0 has collided with the wall 1024x768 WALL if(ball[0].x < 0) ball[0].xmomentum = -ball[0].xmomentum; if(ball[0].x + ball[0].w > 1024) ball[0].xmomentum = -ball[0].xmomentum; if(ball[0].y < 0) ball[0].ymomentum = -ball[0].ymomentum; if(go[0].y + go[0].h > 768) ball[0].ymomentum = -ball[0].ymomentum; This one I can almost figure out because I don't need to include all the other possible balls. [Edited by - Silicon Seed on June 17, 2008 8:44:43 PM]

Share this post


Link to post
Share on other sites
Advertisement
simple, do two for loops, but inside of them check to see if the variable for the first and second are the same, and if so, leave the loop.

for(int x=0;x<1000;x++)
{
for(int y=0;y<1000;y++)
{
if(y!=x)
{
if(ball[x].y + ball[x].h > ball[y].y &&
ball[x].y < ball[y].y + ball[y].h &&
ball[x].x + ball[x].w > ball[y].x &&
ball[x].x < ball[y].x + ball[y].w)
{
//reverse y direction only (blah boring)
ball[0].ymomentum = -ball[0].ymomentum;
}
}
}

I did a simple Darkbasic simulation like this during the school year, and I won a competetion, dont know how I did that though.....

Share this post


Link to post
Share on other sites
Thank you monkey that is exactly what I was looking for! How much are you for sale for? :)


Quote:
Original post by monkey4sale
simple, do two for loops, but inside of them check to see if the variable for the first and second are the same, and if so, leave the loop.

for(int x=0;x<1000;x++)
{
for(int y=0;y<1000;y++)
{
if(y!=x)
{
if(ball[x].y + ball[x].h > ball[y].y &&
ball[x].y < ball[y].y + ball[y].h &&
ball[x].x + ball[x].w > ball[y].x &&
ball[x].x < ball[y].x + ball[y].w)
{
//reverse y direction only (blah boring)
ball[0].ymomentum = -ball[0].ymomentum;
}
}
}

I did a simple Darkbasic simulation like this during the school year, and I won a competetion, dont know how I did that though.....


Share this post


Link to post
Share on other sites
A better way that avoids the if statement and rechecking things that you've already checked is to structure the fors like...


for(int x = 0; x < 1000; x++)
for(int y = x+1; y < 1000; y++)
//checking code is here

Share this post


Link to post
Share on other sites



Of course you could make it go a bit faster and do the movement vector addition in one pass to all the balls (either update the X Y position or put it into a second ball array for 'next position'.


You might also be able to speed it up by only doing half the 1000x1000 iterations by comparing every ball with every other to see if they collide once and skipping comparing a ball with itself as well as with the balls it already checked against

for i = 1 to N // calc new positions for all balls one pass
{
newx = ball.x + ball.vx
newy = ball.y + ball.vy
}

for i = 1 to N

for j = (i+1) to N
{
//test interact ball with ball[j]

// by comparing newx newy newx[j] newy[j]
}


Also you are testing collision as if the balls were square (the box check) and you might use that as a lazy evaluation and then do a more precise calculation after the first test indicated they are likely to collide,

The more precise test is using the distance d = sqrt(dx^2 + dy^2) and seeing
if d is less than the sum of the two balls radius (you can optimize this by compareing (dx^2 + dy^2) with d^2 to eliminate the computationally expensive sqrt() function call.





A more complicated simulation might be to account for simultaneous collision to the same ball by adding forces from all collisions first and then having the balls vectors changed accordingly in the movement resolution phase.

Share this post


Link to post
Share on other sites
This topic interests me too. Without trying to hijack the thread, I'd like to ask something too


Say you have a really big plain in which these shapes are moving. Theoretically, I don't ened to check for collisions with all the shapes. Could I split the plain up in "Areas" and then get away with just checking the Area in which the shape is and the adjescent Areas? How would I keep track of which Area holds which shapes though? I'm guesing with some sort of container full of pointers?


Would it also be safe to say that the probability for every shape to collide with one specific shape is so small, I only need to check the X shapes that are closest to the specific shape? We'd have to calculate X with some probability maths (I'm guessing over normal distribution)


I'm not sure which one of these methods would be more effective speaking in terms of precision and computing power. I'll probably end up in writing a little demo that uses both these methods. I'd just like to hear other peoples opinion about this

Share this post


Link to post
Share on other sites
Original post by starruler
A better way that avoids the if statement and rechecking things that you've already checked is to structure the fors like...

for(int x = 0; x < 1000; x++)
for(int y = x+1; y < 1000; y++)
//checking code is here
[\quote]


with that you would miss all of the early y's, after X reaches say 100 then y would start at 101 and skip all 1-99

Share this post


Link to post
Share on other sites
As a point of intrest, I'd also like to add that you might want to add some kind of flag/mechanism to determine if you've already checked and updated a value as that the possibility of simultanenous multiple collisions can happen, this way you can perform a resolution of the resultant vectors and generate a more realistic scenerio.

Share this post


Link to post
Share on other sites
Quote:
Original post by c4c0d3m0n
This topic interests me too. Without trying to hijack the thread, I'd like to ask something too


Say you have a really big plain in which these shapes are moving. Theoretically, I don't ened to check for collisions with all the shapes. Could I split the plain up in "Areas" and then get away with just checking the Area in which the shape is and the adjescent Areas? How would I keep track of which Area holds which shapes though? I'm guesing with some sort of container full of pointers?

typically yes
Quote:

Would it also be safe to say that the probability for every shape to collide with one specific shape is so small, I only need to check the X shapes that are closest to the specific shape? We'd have to calculate X with some probability maths (I'm guessing over normal distribution)
Typically you use an axis aligned bounding box or some other shape that is easy to check to do a quick check for a entire entity/partial entity before moving on to actual poly to poly collision detection, so yes.
Quote:

I'm not sure which one of these methods would be more effective speaking in terms of precision and computing power. I'll probably end up in writing a little demo that uses both these methods. I'd just like to hear other peoples opinion about this

It really depends on the world you are simulating. Thousands of really close objects the second is faster. Thousands of objects not near each other(and few close objects) the first will help more. Neither should impact precision if implemented properly.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!