EmilMatthew
Squire

China
11 Posts |
Posted - Aug 05 2004 : 07:28:57 AM
|
I 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.
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; }
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.
http://www.flash8.net/bbs/UploadFile/2004-8/20048520718753.swf But after about 10 seconds , you will see the problem occur ,like this.
 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.
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; } }
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! |
Edited by - EmilMatthew on Aug 05 2004 07:30:47 AM
|
|
Eric Coleman
Gladiator
   
USA
811 Posts |
Posted - Aug 05 2004 : 10:03:57 AM
|
Look in the Files section and download Physics.zip by Andy Owen.
|
 |
|
Sion
Warrior
  
Denmark
138 Posts |
Posted - Aug 05 2004 : 10:53:48 AM
|
Man, 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  |
Visit my personal blog at www.AndersNissen.com! |
Edited by - Sion on Aug 05 2004 11:10:43 AM |
 |
|
cbx
Swordmaster
   
Canada
296 Posts |
Posted - Aug 05 2004 : 12:46:10 PM
|
Looks like the blue ball is trying to hump the pink ball.  |
Created by: X http://www.createdbyx.com/ |
 |
|
Spodi
Warrior
  
USA
142 Posts |
Posted - Aug 06 2004 : 05:16:22 AM
|
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. |
vbGORE |
 |
|
Eric Coleman
Gladiator
   
USA
811 Posts |
|
EmilMatthew
Squire

China
11 Posts |
Posted - Aug 06 2004 : 08:50:10 AM
|
Than you ,Eric ,I think I do it better now. I set a function called PreciseCollisionRender to detect whether the two ball is truely apart.
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; } }
And it now is seen like this>> http://www.flash8.net/bbs/UploadFile/2004-8/20048621362191.swf 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>>>
http://www.flash8.net/bbs/UploadFile/2004-8/200486203419957.swf I will try to use your and Andy Owen's method to solve this problem.
|
 |
|
VBBR
Moderator
   
Brazil
617 Posts |
Posted - Aug 06 2004 : 10:32:00 AM
|
quote: 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. (very alive, I would say) |
Whatever. Who knows... |
 |
|