Want a high resolution for two balls's collision..
EmilMatthewI have just finished a trial on Flash with three ball collision.(I am a beginner on DirectX) The core part---Collision and Collision Detect Function is like this. Though it was written using the ActionScript---the Coding Language for Flash,but I don't think there are any difficult for everybody here to understand my programme. [code]function collisiondetect(mc1,mc2){ if(Math.sqrt((mc1._x-mc2._x)*(mc1._x-mc2._x)+(mc1._y- mc2._y)*(mc1._y-mc2._y))<=mc1.r+mc2.r){ collisionx(mc1,mc2); collisiony(mc1,mc2); } } function collisionx(mc1,mc2#65289;{//vx---collision var v1temp=mc1.vx; var v2temp=mc2.vx; mc1.vx=((mc1.m-mc2.m)/(mc1.m+mc2.m))*v1temp+(2*mc2.m/(mc1.m+mc2.m))*v2temp; mc2.vx=(2*mc1.m/(mc1.m+mc2.m))*v1temp-((mc1.m-mc2.m)/(mc1.m+mc2.m))*v2temp; } function collisiony(mc1,mc2){//vy----collision var v1temp=mc1.vy; var v2temp=mc2.vy; mc1.vy=((mc1.m-mc2.m)/(mc1.m+mc2.m))*v1temp+(2*mc2.m/(mc1.m+mc2.m))*v2temp; mc2.vy=(2*mc1.m/(mc1.m+mc2.m))*v1temp-((mc1.m-mc2.m)/(mc1.m+mc2.m))*v2temp; }[/code] As you have seen,I have used a very simple way for the two balls collision detect----that is ----whether the distance between the balls's center is shorter than the sum of the two balls's radius. The flash show is here. [url]http://www.flash8.net/bbs/UploadFile/2004-8/20048520718753.swf[/url] But after about 10 seconds , you will see the problem occur ,like this. [img]http://www.flash8.net/bbs/UploadFile/2004-8/20048520257368.jpg[/img] The reason for this is clear ,because one ball's or both two balls' velocity seem a little bit big ,at one frame time(about 0.03 senconds),so they collision again and again...,seemed like to stick with each other. Of course it's not the thing I want to see. What I think is that there need something to adjust the gradually culumated error. Here is a gravity sample. The adjust part---ball._y=400,promised the ball to stay in ball.y=400 at last. [code]ymov=0; gravity=1; _root.onEnterFrame=function(){//It's just like the timer control in VB. ymov+=gravity; ball._y+=ymov; if(ball._y>400){ ball._y=400;//The adjust part ,very important //to eliminate the error. ymov*=-0.9; } }[/code] So any one here can give a high resolution for two balls's collision to eliminate the errors. Using VB to answer my question is quite OK. Don't because I am using another language so that look down upon me and not answer my quetion. What I searched is the concerpt and way of some game designing,and it's ok through all programme language,right? I love this forum ,thank you![|)]
Eric ColemanLook in the Files section and download Physics.zip by Andy Owen.
SionMan, that's a seriously cool example!! Wow, that good! EDIT: And the best part is that the algorithms are based on techniques used in Hitman 1 (Hitman: Codename 47). For those who don't know it, then Hitman is developed by a danish game developer. (Okay, the developer is now owned by Eidos but it's still danish! Yes it is dammit! [:(] But Hitman 1 - and Hitman 2 for that matter - was made before the takeover.) Anyway, the article on Gamasutra that inspired that algorithm is written by a dane. It's not much, but Denmark only has 4-5 serious game developers (IO Interactive, Media Mobsters, Deadlock (...I think it's called) and Runestone) so I'm a bit proud [;)] Of cause Denmark is going to get another game developer team when I get a bit better and get around to starting a dev. team [:p]
cbxLooks like the blue ball is trying to hump the pink ball. [:p]
SpodiAnd thanks to that we got Little Baby Red Ball who's trying to avoid watching his parents go at it again to make Little Baby Teal Ball.
Eric ColemanOne way to avoid the collision is to push the balls apart after they have collided. From the code you provided, you are only changing the velocity of the balls. The problem is that once a collision occurs the balls are already interlocked, and a change in velocity may or may not break the balls apart during the next iteration of their position. Some pseudo code would look like this. [code] DO ITERATE POSITION IF COLLISION { CALCULATE VELOCITY DO WHILE COLLISION { ITERATE POSITION } } LOOP[/code]
EmilMatthewThan you ,Eric ,I think I do it better now. I set a function called PreciseCollisionRender to detect whether the two ball is truely apart. [code]function PreciseCollisionRender(mc1,mc2){ if(Math.sqrt((mc1._x-mc2._x)*(mc1._x-mc2._x)+(mc1._y-mc2._y)*(mc1._y-mc2._y))>mc1.r+mc2.r+1) { trace("Render on"); mc1.c=0; mc2.c=0; } } function collisiondetect(mc1,mc2){ if(mc1.c==1&&mc2.c==1){ PreciseCollisionRender(mc1,mc2);//*****ok,no problem } else if(Math.sqrt((mc1._x-mc2._x)*(mc1._x-mc2._x)+(mc1._y-mc2._y)*(mc1._y-mc2._y))<=mc1.r+mc2.r){ collisionx(mc1,mc2); collisiony(mc1,mc2); mc1.c=1;mc2.c=1; } }[/code] And it now is seen like this>> [url]http://www.flash8.net/bbs/UploadFile/2004-8/20048621362191.swf[/url] Your idea is good ,I will try it later.[|)] Because my function "PreciseCollisionRender" is a specilized one ,it can't deal with a little more balls,like seven...Like this>>> [url]http://www.flash8.net/bbs/UploadFile/2004-8/200486203419957.swf[/url] I will try to use your and Andy Owen's method to solve this problem.[8D]
Originally posted by Spodi
And thanks to that we got Little Baby Red Ball who's trying to avoid watching his parents go at it again to make Little Baby Teal Ball.
So, you're alive after all. [:p] (very alive, I would say)