sabeeh
Newbie
Offline
Posts: 1


«
Embed this message
Reply #60 on: November 16, 2007, 04:49:44 am » 

Thank you very much for this effort in order to benefit from the computer and the Internet in the area of knowledge transfer



Logged




vanmobi
Newbie
Offline
Posts: 4


«
Embed this message
Reply #61 on: December 19, 2007, 03:52:44 am » 

Hello professor, I am trying to implement the algorithm you described with a small difference: my speed is constantly slowly decreasing so my balls will come to a stop after a while. In your explanation, the speeds are constant. Can you please let me know what needs to be changed to implement my deceleration? Detecting the time the collision will take place is tricky. I guess I need to introduce the notion of acceleration (negative in my case). Thank you!



Logged




colos
Newbie
Offline
Posts: 4


«
Embed this message
Reply #62 on: December 19, 2007, 07:20:11 am » posted from:Taipei,T\'aipei,Taiwan 

The easier way is to introduce a drag force which is proportional to velocity. F= b* V , b and initial velocity will determine how far the ball stop.



Logged




vanmobi
Newbie
Offline
Posts: 4


«
Embed this message
Reply #63 on: December 19, 2007, 08:56:58 pm » 

Thank you. Yes, this seems like the right way of doing it. But I have no idea how to apply this to the existing formulas? 1. What is the impact on the calculation of the component of velocity in the direction of (dx,dy) vp1= vx1 *dx/d+vy1*dy/d; vp2= vx2*dx+vy2*dy/d; 2. How do I compute the collision time? dt =(r1+r2d)/(vp1+vp2);// the collision should have occurred at tdt Thank you for your help! Vanessa



Logged




FuKwun Hwang


«
Embed this message
Reply #64 on: December 19, 2007, 10:51:52 pm » 

Sorry! I misunderstood your question I am trying to implement the algorithm you described with a small difference: my speed is constantly slowly decreasing so my balls will come to a stop after a while. In your explanation, the speeds are constant. I did not look at your previous message carefully. I thought in my case, the total energy of those balls should be constant (energy is conserver) and you want the ball to slow down. It is the opposite. You much have done something wrong so those balls are slowing down. You need to write down how you did it. Otherwise, there is no way I can find out why it is so. For computer code, the time is increases a finite amount. So the ball will change position a finite distance for each time step. At the time you found those two ball collide (d (At the time two ball collide, d is equal to r1+r2). So we need to move the time backward a little so we can find out when those two ball really collide. I use an approximation to find the time dt. (r1+r2d2) is the differences, and vp1,vp2 are velocity almost along the line between center of two ball. So dt=(r1+r2d2)/(vp1+vp2) will move the ball back to the time those two ball just collide.
But your problem has nothing to do with the above miner correction. You must have use the wrong formula for the collision so the speed is decreasing.



Logged




vanmobi
Newbie
Offline
Posts: 4


«
Embed this message
Reply #65 on: December 20, 2007, 01:05:28 am » 

Actually, they currently are not slowing down, but I want them to. Just like in a billiard game, after a while, the balls will stop rolling. That's the behavior I am try to implement. So if I introduce a friction of some sort, what is the implication on the formulas? Thank you, Vanessa



Logged




FuKwun Hwang


«
Embed this message
Reply #66 on: December 21, 2007, 03:20:16 pm » 

O.K. So yo do want to add a friction effect. A simple model: F= m*g*u, which is a constant for each ball. So the distance for a ball to stop will proportional to it's kinetic energy K=(1/2)m*v ^{2}This could be a good model for your case.



Logged




pepsicoder
Newbie
Offline
Posts: 2


«
Embed this message
Reply #67 on: January 26, 2008, 01:40:38 pm » 

vp1= vx1*dx/d+vy1*dy/d; vp2= vx2*dx+vy2*dy/d; // << Is this line a wrong. Should this code be : vp1= vx1*dx/d+vy1*dy/d; vp2= vx2*dx/d+vy2*dy/d;



Logged




FuKwun Hwang


«
Embed this message
Reply #68 on: January 28, 2008, 11:55:35 pm » 

Yes. It should be vp1= vx1*dx/d+vy1*dy/d; vp2= vx2*dx/d+vy2*dy/d; or vp1= (vx1*dx/d+vy1*dy)/d; vp2= (vx2*dx+vy2*dy)/d; It is an inner product between two vector (vx,vy) and (dx/d,dy/d)



Logged




acw
Newbie
Offline
Posts: 1


«
Embed this message
Reply #69 on: April 20, 2008, 03:29:38 am » 

thanks for a really nice site, helped me a lot=) got a few questions: 1. how can i get a applet to work for other regular geometric figures like an rectangle. i cant go with the distance/pythagoras there since the corners will then be ignored. also could you tell me how this is done for iregular figures in short words. (just wanna know basic theori how this is done since this is way over my level). 2. i was able to implement parts of your code into my own applet to make it more realistic, however sometimes the objects get stuck in each other or in the borders. althougth you were very clear in your instructions i wasnt able to fully understand how to keep them from doing that. (something with moving them one step away from each other in the if statement i think?) for(int i = 0;i < 3;i++){ for(int j = i+1;j<4;j++){ if(Math.sqrt((x[i]x[j])*(x[i]x[j])+(y[i]y[j])*(y[i]y[j]))<=30){ int dx=x[j]x[i]; int dy=y[j]y[i]; double d = Math.sqrt(dx*dx+dy*dy); double ax=dx/d, ay=dy/d; double va1=(vx[i]*ax+vy[i]*ay), vb1=(vx[i]*ay+vy[i]*ax); double va2=(vx[j]*ax+vy[j]*ay), vb2=(vx[j]*ay+vy[j]*ax); vx[i]=va2*axvb1*ay; vy[i]=va2*ay+vb1*ax; vx[j]=va1*axvb2*ay; vy[j]=va1*ay+vb2*ax; } } } also would be nice if you could give the physic formula for these lines. i ingnored mass and some other factors that didnt matter in my program since same size on all objects, but would like them in the formula aswell. double ax=dx/d, ay=dy/d; double va1=(vx[i]*ax+vy[i]*ay), vb1=(vx[i]*ay+vy[i]*ax); double va2=(vx[j]*ax+vy[j]*ay), vb2=(vx[j]*ay+vy[j]*ax); Thanks



Logged




FuKwun Hwang


«
Embed this message
Reply #70 on: April 20, 2008, 08:48:39 am » 

1. The boundary of any shape is just connected lines (polygon). You need to check if any two lines from different object connected(in contact) with each other. You can check out ParticlesAndWalls applet: you can draw any shape and particle will be bounded in the region you have created. (Click Usage link under applet to see flash demo). You can check out our EJS code and find out how it was implemented. 2. You should write down formulas for your code. Your code should be based on your equations.



Logged




amol
Newbie
Offline
Posts: 4


«
Embed this message
Reply #71 on: May 07, 2008, 12:42:17 pm » posted from:Bombay,Maharashtra,India 

Hi professor, grt site..... i really like ur work and the way u r carrying it.. thanx and good luck.. also looking forward for the post for gaming people



Logged




amol
Newbie
Offline
Posts: 4


«
Embed this message
Reply #72 on: May 07, 2008, 11:29:46 pm » 

Hello sir, i have tried to make the collision of two balls. I have followed the steps u have written in some previous post. in my case i have kept one ball at rest, and other moving. But after collision the behaviour of both the ball is weired. here is my code int dx = ball[1].x  ball[0].x; int dy = ball[1].y  ball[0].y; double dist = MathHelper::sqrt(dx*dx + dy*dy); //Unit vector in the direction of collision double ax = dx /dist; double ay = dy/dist; if(dist < 10) // (actually r1+r2 = 10) { // Projection of the velocities in these axes double va1=(ball[0].vx*ax+ball[0].vy*ay), vb1=(ball[0].vy*ay+ball[0].vy*ax); double va2=(ball[1].vx*ax+ball[1].vy*ay), vb2=(ball[1].vx*ay+ball[1].vy*ax); // New velocities in these axes (after collision): ed<=1, for elastic collision ed=1 double vaP1=va1 + (1+1)*(va2va1)/(1+1); double vaP2=va2 + (1+1)*(va1va2)/(1+1); // Undo the projections ball[0].vx=(int)(vaP1*axvb1*ay); ball[0].vy=(int)(vaP1*ay+vb1*ax);// new vx,vy for ball 1 after collision ball[1].vx=(int)(vaP2*axvb2*ay); ball[1].vy=(int)(vaP2*ay+vb2*ax);// new vx,vy for ball 2 after collision } Please comment on this. thanx.



Logged




FuKwun Hwang


«
Embed this message
Reply #73 on: May 08, 2008, 12:04:09 am » 

I would suggest you check the momentum and energy just before and right after the collision. If it is the same, then there is nothing wrong with the collision. After the collision the behavior of both balls are wired, may be it is not due to collision , it is due to some other code. And what do you mean by wired? There is no way to help without detail information.



Logged




amol
Newbie
Offline
Posts: 4


«
Embed this message
Reply #74 on: May 08, 2008, 02:38:01 am » posted from:Bombay,Maharashtra,India 

Hello sir, The motion of two ball is weired means they stick each other and after that there is no movement for both the ball. Sometimes after hitting one ball stops and other keep moving. when they stop moving at that time velocities i got for ball in x and y directions are 0 and ball stops. // Projection of the velocities in these axes double va1=(ball[0].vx*ax+ball[0].vy*ay), vb1=(ball[0].vy*ay+ball[0].vy*ax); double va2=(ball[1].vx*ax+ball[1].vy*ay), vb2=(ball[1].vx*ay+ball[1].vy*ax); // New velocities in these axes (after collision): ed<=1, for elastic collision ed=1 double vaP1=va1 + (1+1)*(va2va1)/(1+1); double vaP2=va2 + (1+1)*(va1va2)/(1+1); // Undo the projections ball[0].vx=(int)(vaP1*axvb1*ay); ball[0].vy=(int)(vaP1*ay+vb1*ax);// new vx,vy for ball 1 after collision ball[1].vx=(int)(vaP2*axvb2*ay); ball[1].vy=(int)(vaP2*ay+vb2*ax);// new vx,vy for ball 2 after collision Here i got the veocities 0 and ball stops. But rest of the time collision is perfect. Thank you.


« Last Edit: May 08, 2008, 02:14:41 pm by amol »

Logged




FuKwun Hwang


«
Embed this message
Reply #75 on: May 08, 2008, 11:17:30 pm » 

(From the information you have provided in the above message). I think your problem is due to you did not correct the small time different between collision real happened and the time your program detect the collision. The condition used to detect the collision in your code is dist< r1+r2 However, collision occurred when dist==r1+r2. So there is a small error. If the time step is small enough, the error can be ignored. The problem is worse for programming in flash actionscript(because the time step is too large, 12fps?) Please check out another message at this topic: http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.msg15#msg15and you will find out how to correct it (another approximation!).



Logged




ObsessedOne
Newbie
Offline
Posts: 4


«
Embed this message
Reply #76 on: May 18, 2008, 01:59:02 am » 

Hello Professor Hwang I have been working on this simulation for quite a while now. I feel that I am very, very close. I still have the time error where they clutch together but they aren't completely stuck. Most of the time they bounce, but other times they do not. Do you see anything wrong with what I have now? I assure you it's very readable. Note: In this case each orb is completely responsible for itself only so infinitely many orbs can be added. There are other methods and classes which I have not shown for sake of space and simplicity. public void draw(Graphics gBuffer, Graphics g, Image vm) { gBuffer.setColor(color); gBuffer.fillOval(x  velocity, y  velocity, 40, 40); gBuffer.setColor(Color.black); gBuffer.drawOval(x  velocity, y  velocity, 40, 40); gBuffer.drawImage(vm,0,0,this); // Here is one point I am wondering about long startTime = 0, lastTime; long delay=50, delta; lastTime = startTime = System.currentTimeMillis(); collision = false; orbPhysics(); space.repaint(); } public void orbPhysics() { // Here is another point I am unsure of delay = 1; delta = System.currentTimeMillis()lastTime; lastTime+=delta; collisionPhysics(delta/1000); startTime+=delay; bouncePhysics(); // edge of screen bouncing method (not shown) } // variables not shown public void collisionPhysics(double dt) { ArrayList<Orb> orbs = Space.orbList; el = 1; for(Orb orb : orbs) { r1 = radius; m1 = radius * radius; x1 = x + diameter/2; y1 = y + diameter/2; vx1 = xv; vy1 = yv; Orb other = orb; r2 = other.getRadius(); m2 = other.getRadius() * other.getRadius(); x2 = other.getX() + other.getDiameter()/2; y2 = other.getY() + other.getDiameter()/2; vy2 = other.getVelocityY(); if(x1!=x2 && y1!=y2) // I don't know if there is a better way to test orbs for equality { dx = x1x2; dy = y1y2; d = (Math.sqrt(dx*dx+dy*dy)); if(d < r1+r2) { vp1 = vx1*dx/d + vy1*dy/d; vp2 = vx2*dx/d + vy2*dy/d; // Here is another point I am unsure of ddt = (r1+r2)/(vp1+vp2); if(ddt > dt) ddt = 0; x1=vx1*ddt; y1=vy1*ddt; x2=vx2*ddt; y2=vy2*ddt; vp1 = vx1*dx/d + vy1*dy/d; vp2 = vx2*dx/d + vy2*dy/d; collision = true; otherOrb = other; } if(collision) { ax = dx/d; ay = dy/d; va1 = vx1*ax + vy1*ay; vb1 = vx1*ay + vy1*ax; va2 = vx2*ax + vy2*ay; vb2 = vx2*ay + vy2*ax; vaP1 = va1 + (1+el)*(va2va1)/(1+m1/m2); vaP2 = va2 + (1+el)*(va1va2)/(1+m2/m1); vx1 = vaP1*axvb1*ay; vy1 = vaP1*ay+vb1*ax; vx2 = vaP2*axvb2*ay; vy2 = vaP2*ay+vb2*ax; xv = vx1; yv = vy1; otherOrb.setVelocityX(); otherOrb.setVelocityY(); collision = false; } } } }



Logged




FuKwun Hwang


«
Embed this message
Reply #77 on: May 18, 2008, 10:09:32 pm » 

dt =(r1+r2d)/(vp1+vp2);// the collision should have occurred at tdt (Actually this is also an approximation). However, your formula is ddt=(r1+r2)/(vp1+vp2); Please check out http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.msg15#msg15 I did not write java code directly since I knew about EJS. Because I use it to create java simulation (It will save me a lot of time). I am going to show you the way I used to create java simulation: public class javafilename extends java.applet.Applet implements Runnable{ ... public void start(){ //Start animating! if (animThread == null) { animThread = new Thread(this); animThread.start(); } //Remember the starting time. of thread lastTime=startTime = System.currentTimeMillis(); } public void stop() { //Stop the animating thread. animThread = null; running=false; } public void run() { //Just to be nice, lower this thread's priority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //This is the animation loop. while (Thread.currentThread() == animThread) { //Advance the animation frame. with delta time delta=System.currentTimeMillis()lastTime; lastTime+=delta; if(running)advanced(delta/1000.); startTime+=delay; try { animThread.sleep(Math.max(0,startTimeSystem.currentTimeMillis())); } catch (InterruptedException e) { break; } } } void advanced(double dt){// code to move particle from t to t+dt ... } ... }



Logged




ObsessedOne
Newbie
Offline
Posts: 4


«
Embed this message
Reply #78 on: May 21, 2008, 09:37:16 am » 

Hello again, I have been trying to understand your code, but I know very little since I'm just a first year Computer Science student. I have read all your messages but my greatest problem is understanding the identifiers. I have tried your time change methods and they no longer clutch, but they still aren't colliding well. What is vx0/vy0? How does the variable "time" affect the program? What is the difference between dt and ddt? Why is delta divided by 1000 when it is passed? Does pauseID being equal to 1 indicate a collision? How is vn1/vn2 used? What is eta? (elasticity/energy?) Here is my program since the last post: public Orb(Space s, int orbX, int orbY, int diameter, int speed) { space = s; x = orbX; y = orbY; radius = diameter/2; velocity = speed; xMax = space.getAppletWidth()diameter; yMax = space.getAppletHeight()diameter; // This is not updated. I'm assuming it's supposed to only initialize. time = 0; delay = 50; startTime = 0; lastTime = startTime = System.currentTimeMillis();
} public void draw(Graphics gBuffer, Image vm) { // This is updated every time the ball moves. I'm assuming this is correct. delta = System.currentTimeMillis()lastTime; lastTime += delta; orbPhysics(delta); startTime += delay; space.repaint(); } public void orbPhysics(double dt) { dt/=1000; ArrayList<Orb> orbs = space.getOrbList(); el = 1; for(Orb orb : orbs) { Orb other = orb; if(x1!=x2 && y1!=y2) { dx = x1x2; dy = y1y2; d = (Math.sqrt(dx*dx+dy*dy)); if(d < r1+r2) { vp1 = vx1*dx/d + vy1*dy/d; vp2 = vx2*dx/d + vy2*dy/d; // I'm not sure if this is the way to move the particles. I use x and y for their coordinates ddt = (r1+r2d)/(vp1+vp2); if(ddt > dt) ddt = 0; x1=vx1*ddt; y1=vy1*ddt; x2=vx2*ddt; y2=vy2*ddt; x = (int)x1radius; y = (int)y1radius; other.setX((int)x2other.getRadius()); other.setY((int)y2other.getRadius()); collision = true; otherOrb = other; } if(collision) { ax = dx/d; ay = dy/d; va1 = vx1*ax + vy1*ay; vb1 = vx1*ay + vy1*ax; va2 = vx2*ax + vy2*ay; vb2 = vx2*ay + vy2*ax; vaP1 = va1 + (1+el)*(va2va1)/(1+m1/m2); vaP2 = va2 + (1+el)*(va1va2)/(1+m2/m1); vx1 = vaP1*ax  vb1*ay; vy1 = vaP1*ay + vb1*ax; vx2 = vaP2*ax  vb2*ay; vy2 = vaP2*ay + vb2*ax; // I think they should be moved forward in time here.
xv = vx1; yv = vy1; otherOrb.setVelocityX(vx2); otherOrb.setVelocityY(vy2); collision = false; } } } }



Logged




FuKwun Hwang


«
Embed this message
Reply #79 on: May 21, 2008, 05:11:44 pm » posted from:Taipei,T\'aipei,Taiwan 

What is vx0/vy0? Because user can change the coordinate system, vx0,vy0 is the offset when changing the system. In your case, both of them will be 0. How does the variable "time" affect the program? time stored the real time value in second. it will starts to count when you click Start button. What is the difference between dt and ddt?dt is the time step in second. ddt is used for correct the time step, because when you detect two particle collide (d12 We need to move time backward to d12==r1+r2 and ddt is the time needed to move particle backward. Did you read the posted message I have suggested ?
Why is delta divided by 1000 when it is passed? delta is the time in millisecond (ms).
Does pauseID being equal to 1 indicate a collision? If the checkbox cont. is not checked, the simulation will paused when collision occurred.
How is vn1/vn2 used? Calculate the normal components of the velocity.
What is eta? (elasticity/energy?) Yes. for inelastic collision. it can be ranged between 01.
The way you are coding is not the standard way. You should used the method I have suggested (which will give you almost the same simulation regarding different computer speed).



Logged




ObsessedOne
Newbie
Offline
Posts: 4


«
Embed this message
Reply #80 on: May 22, 2008, 10:25:33 am » 

Yes! I have it! Once I understood what the difference between dt and ddt was I understood everything perfectly. Now I will concentrate on efficiency over readability. Thank you very much Professor Hwang!



Logged




ObsessedOne
Newbie
Offline
Posts: 4


«
Embed this message
Reply #81 on: May 26, 2008, 04:53:10 am » 

Sorry to ask a nonphysics related question, but how did you set the coordinates for your Choice menus? Mine default to the top center of my applet.



Logged




FuKwun Hwang


«
Embed this message
Reply #82 on: May 26, 2008, 08:47:54 am » 

You need to set the LAYOUT for the applet. Please search with "applet layout".



Logged




gap89
Newbie
Offline
Posts: 2


«
Embed this message
Reply #83 on: June 10, 2008, 04:16:41 pm » 

Hello Professor. Normally we are calculating the resultant vector from the initial velocity. Is it possible to do it the inverse way? assuming that the second object is stationary. Which just means I am trying to calculate the vector needed to collide with a stationary object to push the second object in a specific direction.



Logged




FuKwun Hwang


«
Embed this message
Reply #84 on: June 10, 2008, 11:55:37 pm » 

Yes. You can! Write down the equation for conservation of momentum and conservation of energy, then solve it!



Logged




gap89
Newbie
Offline
Posts: 2


«
Embed this message
Reply #85 on: June 11, 2008, 02:13:07 pm » 

Hmm.. I am still quite lost. I tried doing the inverse way by using the equations you have given, as I am using it for programming too, but it was hard to get the answer. Since object 2 is stationary, va2 and vb2 should be 0. Assuming I know what is the final vx2 and vx1, and also getting vap1 = 0.05*va1 ( ed = 0.9 ), vap2 = 0.95 * va1, so how do I continue from here? Or I can't do it this way?



Logged




FuKwun Hwang


«
Embed this message
Reply #86 on: June 11, 2008, 05:06:44 pm » posted from:Taipei,T\'aipei,Taiwan 

You should derive new equations instead of working from the equations in the current simulation. It is a different problem and the equations will be different. Please try to derive the equation from conservation of momentum and conservation of energy. And solve the combined equations. If your condition is push the second into a specific direction and no more, there are many possible solution (not just one). Because for 2D case, you only have 3 equations. But you have 5 unknown variables.



Logged




HaydenG
Newbie
Offline
Posts: 1


«
Embed this message
Reply #87 on: June 18, 2008, 01:11:51 pm » 

Hi, Im sorry to bother you buy could you please tell me what is wrong with this equation I apologize im not very good at programming Public Type PhysObj X As Double Y As Double Xv As Double Yv As Double m As Integer 'mass r As Integer 'radius End Type Public Function BallTouch(a As PhysObj, b As PhysObj, ed As Integer) Dim dx As Double Dim dy As Double Dim d As Double Dim ax As Double Dim ay As Double Dim ava As Double Dim avb As Double Dim bva As Double Dim bvb As Double Dim vapa As Double Dim vapb As Double Dim vpa As Double Dim vpb As Double Dim dt As Double dx = a.X  b.X dy = a.Y  b.Y d = Sqr((dx * dx) + (dy * dy)) ax = dx / d ay = dy / 2 ava = (a.Xv * ax) + (b.Yv * ay) avb = (a.Xv * ay) + (a.Yv * ax) bva = (b.Xv * ax) + (b.Yv * ay) bvb = (b.Xv * ay) + (b.Yv * ax) vapa = ava + (1 + ed) * (bva  ava) / (1 + (a.m / b.m)) vapb = bva + (1 + ed) * (ava  bva) / (1 + (b.m / a.m)) vpa = (a.Xv * dx) / d + (a.Yv * dy) / d vpb = (b.Xv * dx) / d + (b.Yv * dy) / d dt = (a.r + b.r  d) / (vpa + vpb) If d < a.r Then a.X = a.X  (a.Xv * dt) b.X = b.X  (b.Xv * dt) a.Y = a.Y  (a.Yv * dt) b.Y = b.Y  (b.Yv * dt) a.Xv = vapa * ax  avb * ay a.Yv = vapa * ay + avb * ax b.Xv = vapb * ax  bvb * ay b.Yv = vapb * ay + bvb * ax a.X = a.X + (a.Xv * dt) b.X = b.X + (b.Xv * dt) a.Y = a.Y + (a.Yv * dt) b.Y = b.Y + (b.Yv * dt) End If End Function Thank you



Logged




FuKwun Hwang


«
Embed this message
Reply #88 on: June 18, 2008, 07:31:06 pm » 

Why ay = dy / 2



Logged




saeky123
Newbie
Offline
Posts: 1


«
Embed this message
Reply #89 on: July 19, 2008, 06:24:28 pm » 

Hi professor, and thanks for your help... I have followed your post. But I have a question, have you ever worked on collision in 2d with 3 or more balls. I means that collision with more than 2 balls. In this case, I can't use d for distance between 2 balls. I wondered if I use a matrix that store all the boundary of the balls. So that, when the balls collide, the matrix will meet together. Or, maybe, I have to store many d for distance between balls. For example, if I have 3 balls, I use d1 for distance between ball 1 and 2, d2( for ball 2 and 3) and d3(of course, distance between ball 1 and 3). Can you give me any suggestions?



Logged




