<?
import java
.awt.*;

public class 
Bounce extends Animation {
    
    protected 
double X1X2Y1Y2Vx1Vy1Vx2Vy2dXdYvectorXvectorYVa1Va2Vb1Vb2VaP1VaP2,
                        
deltaTdeltaBdistancemetersPerPixelgravityelasticColl;
    protected 
int radius1radius2firstTime=1pixelX1pixelY1pixelX2pixelY2mass1mass2;
    protected 
Color color1 Color.white;
    protected 
Color color2 Color.red;
    
    protected 
void initAnimator() {
        
deltaT 0.0005;// simulation time interval in seconds
        
setDelay((int)(1000*deltaT)); // needed for Animation superclass
        
        
gravity 20;
        
        
elasticColl 1.5;
        
        
mass1 10;
        
mass2 20;
        
        
X1 1// in meters
        
Y1 2// Y reference direction downwards!
        
X2 4;
        
Y2 5;
        
Vx1 4// in m/s
        
Vy1 = -8;
        
Vx2 = -5// in m/s
        
Vy2 = -1.3;
        
        
metersPerPixel 40;
        
        
radius1 20// in pixels!
        
radius2 40;
        
        
pixelX1 = (int) (metersPerPixel X1); // screen position
        
pixelY1 = (int) (metersPerPixel Y1);
        
pixelX2 = (int) (metersPerPixel X2); // screen position
        
pixelY2 = (int) (metersPerPixel Y2);
    }
    
    protected 
void paintAnimator(Graphics g) {
        
        
g.setColor(Color.black);
        if(
firstTime==1) {
            
g.fillRect(0,0,d.width,d.height); 
            
firstTime=0;
        }
        
        
g.fillOval(pixelX1 radius1pixelY1 radius1radius1 2radius1 2);
        
g.fillOval(pixelX2 radius2pixelY2 radius2radius2 2radius2 2);

        
//collision with walls
        
if (pixelX1 radius1 || pixelX1 d.width radius1) {
            
Vx1 = -Vx1;
        }
        if (
pixelY1 radius1 || pixelY1 d.height radius1) {
            
Vy1 = -Vy1;
        }
        if (
pixelX2 radius2 || pixelX2 d.width radius2) {
            
Vx2 = -Vx2;
        }
        if (
pixelY2 radius2 || pixelY2 d.height radius2) {
            
Vy2 = -Vy2;
        }
        
        
//Apply the gravity on the balls
        
Vy1 Vy1+gravity*deltaT;
        
Vy2 Vy2+gravity*deltaT
        
        
//calculate the distance between the center of the balls using pythagoras theorem
        //to check see if the balls collide
        
dX pixelX2-pixelX1
        
dY pixelY2-pixelY1;
        
distance Math.sqrt(dX*dX+dY*dY);     
        
        
//collision check
        
if (distance radius1+radius2) {    
            
            
//Calculate the vector towards the center of the ball (in the direction of the collision)
            
vectorX dX/distance;
            
vectorY dY/distance;
            
            
//The new velocities in x- and y-axes of the collision
            
Va1 = (Vx1*vectorX Vy1*vectorY);
            
Vb1 = (-Vx1*vectorY Vy1*vectorX);
            
Va2 = (Vx2*vectorX Vy2*vectorY);
            
Vb2 = (-Vx2*vectorY Vy2*vectorX);
            
            
//The new velocities after collision
            
VaP1 Va1 elasticColl*(Va2-Va1)/(1+mass1/mass2);
            
VaP2 Va2 elasticColl*(Va1-Va2)/(1+mass2/mass1);
            
            
//Undo the projections
            
Vx1 VaP1*vectorX Vb1*vectorY;
            
Vy1 VaP1*vectorY Vb1*vectorX;
            
Vx2 VaP2*vectorX Vb2*vectorY;
            
Vy2 VaP2*vectorY Vb2*vectorX;        
        }    
            
        
X1 += Vx1 deltaT;
        
Y1 += Vy1 deltaT;
        
X2 += Vx2 deltaT;
        
Y2 += Vy2 deltaT;
        
        
pixelX1 = (int) (metersPerPixel X1);
        
pixelY1 = (int) (metersPerPixel Y1);
        
pixelX2 = (int) (metersPerPixel X2); 
        
pixelY2 = (int) (metersPerPixel Y2);
        
        
//paint the balls
        
g.setColor(color1);
        
g.fillOval(pixelX1 radius1pixelY1 radius1radius1 2radius1 2);
        
g.setColor(color2);
        
g.fillOval(pixelX2 radius2pixelY2 radius2radius2 2radius2 2);
    }
}
?>
Java problem: bouncing balls - TranceAddict Forums

Become a part of the TranceAddict community!Frequently Asked Questions - Please read this if you haven'tSearch the forums
TranceAddict Forums > Main Forums > Chill Out Room > Java problem: bouncing balls
  Last Thread   Next Thread
Share
Author
Thread    Post A Reply
Cloudburst
I am the maximum



Registered: Oct 2003
Location: Jötebårj
Java problem: bouncing balls

I dunno if there's any Java geeks in here (tell me where I could get help if you know). Anyway this might be more of a physics problem. I've gotten 2 balls bouncing good (assignment in a math course), but only when one balls weight is twice as big as the other one's. I want to preserve the energy in the collisions.

So:

if
mass1 = 2*mass2 or mass2 = 2*mass1 and elasticColl = 1.5
everything works fine. I'm not sure what's wrong.

CLICK HERE FOR APPLET

If anyone feels they could find the problem (I doubt it because it has to be done in a few hours), feel free to try.

PHP:


___________________



insignificant cor member alliance

Old Post Feb-12-2006 18:37  Finland
Click Here to See the Profile for Cloudburst Click here to Send Cloudburst a Private Message Visit Cloudburst's homepage! Add Cloudburst to your buddy list Report this Post Reply w/Quote Edit/Delete Message
DjDeComp
Proud V-Dub Owner



Registered: Jul 2002
Location: New Jersey, USA

yes


___________________
PLTA Forums

Old Post Feb-12-2006 18:44  Poland
Click Here to See the Profile for DjDeComp Click here to Send DjDeComp a Private Message Add DjDeComp to your buddy list Report this Post Reply w/Quote Edit/Delete Message
Cloudburst
I am the maximum



Registered: Oct 2003
Location: Jötebårj

Fucking hell...you can tell I'm desperate... I'M ASKING THE COR FOR HELP LOL.

Bah... waste of time I guess...


___________________



insignificant cor member alliance

Old Post Feb-12-2006 18:46  Finland
Click Here to See the Profile for Cloudburst Click here to Send Cloudburst a Private Message Visit Cloudburst's homepage! Add Cloudburst to your buddy list Report this Post Reply w/Quote Edit/Delete Message
Coup
Retired



Registered: May 2001
Location: England, UK

quote:
Originally posted by DjDeComp
yes

+1

Old Post Feb-12-2006 18:47  England
Click Here to See the Profile for Coup Click here to Send Coup a Private Message Add Coup to your buddy list Report this Post Reply w/Quote Edit/Delete Message
stevieboy32808
==============



Registered: Mar 2005
Location: United States

Collision and energy remind me a lot of these equations:

Momentum = mass*velocity <---Applies to collisions

kintetic energy(KE) = 1/2*(mass)(velocity)^2

You say you want to preserve the energy so somehow coming up with an equation relevant to the above you can set it equal to zero. Do you have a book outlining some sort of procedure and do you understand what I'm trying to say?

Old Post Feb-12-2006 18:52 
Click Here to See the Profile for stevieboy32808 Click here to Send stevieboy32808 a Private Message Add stevieboy32808 to your buddy list Report this Post Reply w/Quote Edit/Delete Message
DjDeComp
Proud V-Dub Owner



Registered: Jul 2002
Location: New Jersey, USA

quote:
Originally posted by Cloudburst
Fucking hell...you can tell I'm desperate... I'M ASKING THE COR FOR HELP LOL.

Bah... waste of time I guess...


desperate.. yes

wasting your time.. no


___________________
PLTA Forums

Old Post Feb-12-2006 18:55  Poland
Click Here to See the Profile for DjDeComp Click here to Send DjDeComp a Private Message Add DjDeComp to your buddy list Report this Post Reply w/Quote Edit/Delete Message
Sunsnail
Global Moderator



Registered: Sep 2004
Location:

you may want to check this site out

http://javahelp.com/mass

Old Post Feb-12-2006 19:29 
Click Here to See the Profile for Sunsnail Click here to Send Sunsnail a Private Message Add Sunsnail to your buddy list Report this Post Reply w/Quote Edit/Delete Message

TranceAddict Forums > Main Forums > Chill Out Room > Java problem: bouncing balls
Post New Thread    Post A Reply

 
Last Thread   Next Thread
Click here to listen to the sample!Pause playbackAcid 303 Bassline Trance from The 90's ID [2012] [0]

Click here to listen to the sample!Pause playbackCape Town - Pitstop [2002]

Show Printable Version | Subscribe to this Thread
Forum Jump:

All times are GMT. The time now is 07:19.

Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
HTML code is ON
vB code is ON
[IMG] code is ON
 
Search this Thread:

 
Contact Us - return to tranceaddict

Powered by: Trance Music & vBulletin Forums
Copyright ©2000-2026, Jelsoft Enterprises Ltd.
Privacy Statement / DMCA
Support TA!