Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 27th, 2005, 12:19 AM   #1
WCentauri
Newbie
 
Join Date: May 2005
Posts: 2
Rep Power: 0 WCentauri is on a distinguished road
Send a message via AIM to WCentauri
Please consider this java applet

Hi all. I love to program and would like to find a place where I can shoot the shit with other programmers. I have been working on this little java applet that models gravitational interaction between massless test particles and what I call great attractors. It would be great if I could get some feedback on this thing. The code below has two programs: pMass.java and bbattractor.java, which is the applet.

//pMass.java
import java.awt.*;

public class pMass
{
	public int r;
	public int color;
	public int x;
	public int y;
	public int vx;
	public int vy;
	public int ax;
	public int ay;
	
	public pMass(int R, int X, int Y)
	{	r=R;
		x=X;
		y=Y;
		chooseColor();
	}
	
	private void chooseColor()
	{
		int num=(int)(7*Math.random());
		if(num==0) color=0;
		else if(num==1) color=1; 
		else if(num==2) color=2;
		else if(num==3) color=3;
		else if(num==4) color=4;
		else if(num==5) color=5;
		else color=6;
	}

	public void draw(Graphics thing)
	{
		Color drawColor;
		if(color==0)		drawColor=Color.blue;
		else if(color==1) drawColor=Color.cyan; 
		else if(color==2) drawColor=Color.magenta;
		else if(color==3) drawColor=Color.orange;
		else if(color==4) drawColor=Color.pink;
		else if(color==5) drawColor=Color.red;
		else 					drawColor=Color.yellow;

		thing.setColor(drawColor);
		thing.fillOval(x-r,y-r,2*r,2*r);
	}
	
	public int getR()
	{	return r;}
	
	public int getX()
	{	return x;}
	
	public int getY()
	{	return y;}
	
	public int vxO() //'O' as in "oh", not "zero"
	{	return vx;}
	
	public int vyO()
	{	return vy;}
	
	public void vxI(int v)
	{	vx=v;}
	
	public void vyI(int v)
	{	vy=v;}
	
	public int axO() 
	{	return ax;}
	
	public int ayO()
	{	return ay;}
	
	public void axI(int a)
	{	ax=a;}
	
	public void ayI(int a)
	{	ay=a;}

	public void move()
	{
		vx+=ax;
		vy+=ay;	
		x+=vx;
		y+=vy;
	}
}
Here's the second part.
//bbatttractor.java
//applet of test particle and a moving great attractor
//5-19-2005 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;

public class bbattractor extends Applet
{	
	//applet
	private int width=1150;
	private int height=750;
	private boolean valid_position=true;
	//pMass objects
	private int r=5;
	private int attR=1;
	private pMass[] mass=new pMass[10];
	private pMass[] att=new pMass[1];
	private int ds=5;
	//animation 
	private Timer timer;
	private Image image;
	private int delay=1;
	
	public void init()
	{
		setBackground(Color.black);
		setSize(width,height);

		addMouseListener(new AMouseListener());
      timer = new Timer(delay, new AnActionListener());
      timer.start();
	//create non-overlapping test particles
		for(int i=0; i < mass.length; i++)
		{
			int dx=0,x=0,dy=0,y=0;
			do{
				dx=width-2*r;
				x=r+(int)(dx*Math.random());
				dy=height-2*r;
				y=r+(int)(dy*Math.random());
				for(int k=0; k < i; k++)
				{
					if (!overlap(r,x,y,mass[k].getR(),mass[k].getX(),mass[k].getY() )) valid_position=true;
					else {valid_position=false; break;}
				}
			}while(!valid_position);
			mass[i]=new pMass(r,x,y);
			//if(i==(mass.length-1)) mass[mass.length-1]=new pMass(1,x,y);
		}
		//positions for attractors
		for(int i=0; i<att.length; i++)
		{	int dx=0,x=0,dy=0,y=0;
			do{
				dx=width-2*attR;
				x=attR+(int)(dx*Math.random());
				dy=height-2*attR;
				y=attR+(int)(dy*Math.random());
				for(int k=0; k < i; k++)
				{
					if (!overlap(r,x,y,att[k].getR(),att[k].getX(),att[k].getY() )) valid_position=true;
					else {valid_position=false; break;}
				}
			}while(!valid_position);
			att[i]=new pMass(attR,x,y);
		}


	//assign velocities
		//mass[0].vxI(1); mass[0].vyI(1);
		int v;
		for(int i=0; i < mass.length; i++)
		{	// x-velocity
			v=1+(int)(ds*Math.random());
			if (Math.random() >= 0.5) v=-v;
			mass[i].vxI(v);

			//y-velocity
			v=1+(int)(ds*Math.random());
			if (Math.random() >= 0.5) v=-v;
			mass[i].vyI(v);	
		}	
		for(int i=0; i<att.length; i++)
		{	// x-velocity
			v=1+(int)(ds*Math.random());
			if (Math.random() >= 0.5) v=-v;
			att[i].vxI(v);

			//y-velocity
			v=1+(int)(ds*Math.random());
			if (Math.random() >= 0.5) v=-v;
			att[i].vyI(v);
		}	
	}
	
	public void paint(Graphics thing)
	{
		for(int i=0; i < mass.length; i++) mass[i].draw(thing);
		for(int i=0; i< att.length; i++) att[i].draw(thing);
		thing.setColor(Color.green);
		thing.drawRect(0,0,width,height);
	}

	public void move()
	{
		for(int i=0; i<mass.length; i++)
		{	int dax=0,day=0;
			for(int k=0; k<att.length; k++)
			{	int dx=mass[i].getX()-att[k].getX();
				int dy=mass[i].getY()-att[k].getY();
				int rSqre=dx*dx+dy*dy;
				double theta;
				if(dx==0) {theta=Math.PI/2;}
				else {theta=Math.atan(dy/dx);}
				if(dx<=0 && dy>0) theta+=Math.PI;
				if(dx<=0 && dy<0) theta-=Math.PI;
				
				dax+=(-(int)(10000*Math.cos(theta)/rSqre));
				day+=(-(int)(10000*Math.sin(theta)/rSqre));
			}
			mass[i].axI(dax);
			mass[i].ayI(day);
		}
			
		//boundaries and particles
		for(int i=0; i<mass.length; i++)
		{
			//walls-vertical boundaries
     		if(mass[i].getX() > width - mass[i].getR() && mass[i].vxO() > 0)
				{	int v=(int)(ds*Math.random());	
					mass[i].vxI(-v-1); 
     				v=(int)((ds)*Math.random());
					if (Math.random() >= 0.5) v=-v;
					mass[i].vyI(v);
				}
			if(mass[i].getX() < mass[i].getR() && mass[i].vxO() < 0)
				{	int v=(int)(ds*Math.random());	
					mass[i].vxI(v+1); 
     				v=(int)((ds)*Math.random());
					if (Math.random() >= 0.5) v=-v;
					mass[i].vyI(v);
				}
				
	  		//walls-horizontal boundaries		
			if(mass[i].getY() < mass[i].getR() && mass[i].vyO() < 0)
      		{	int v=(int)((ds)*Math.random());
					mass[i].vyI(v+1); 
     				v=(int)((ds)*Math.random());
					if (Math.random() >= 0.5) v=-v;
					mass[i].vxI(v);
				}
			if(mass[i].getY() > height - mass[i].getR() && mass[i].vyO() > 0)
				{	int v=(int)((ds)*Math.random());
					mass[i].vyI(-v-1); 
     				v=(int)((ds)*Math.random());
					if (Math.random() >= 0.5) v=-v;
					mass[i].vxI(v);
				}
			}
			
			//collisions among particles
			for(int i=0;i<mass.length; i++)
			{	
				for(int k=0;k<i;k++)
				if(k!=i && overlap(mass[i].getR(),mass[i].getX(),mass[i].getY(),mass[k].getR(),mass[k].getX(),mass[k].getY()) )
				{	
					if(mass[i].vx*mass[k].vx < 0) //horizontal direction
						{	mass[i].vx=-mass[i].vx;
     						mass[k].vx=-mass[k].vx;
							
							mass[i].x+=mass[i].vx;
							mass[k].x+=mass[k].vx; //move one particle away to prevent prolonged collision
						}
					else if( mass[i].vx*mass[i].vx >= mass[k].vx*mass[k].vx ) 
						{	mass[i].vx=-mass[i].vx;  //same direction; switch velocities
							
							mass[i].x+=mass[i].vx;
							mass[k].x+=mass[k].vx;
						}
					else 
						{	mass[k].vx=-mass[k].vx;
							
							mass[i].x+=mass[i].vx;
							mass[k].x+=mass[k].vx;
						}
					
					if(mass[i].vy*mass[i].vy < 0) //vertical direction
						{	mass[i].vy=-mass[i].vy;
        					mass[k].vy=-mass[k].vy;

							mass[i].y+=mass[i].vy;
							mass[k].y+=mass[k].vy;
						}
					else if( mass[i].vy*mass[i].vy >= mass[k].vy*mass[k].vy ) 
						{	mass[i].vy=-mass[i].vy; //same direction; switch velocities
						
							mass[i].y+=mass[i].vy;
							mass[k].y+=mass[k].vy;
						}
					else 
						{	mass[k].vy=-mass[k].vy;
						
							mass[i].y+=mass[i].vy;
							mass[k].y+=mass[k].vy;
						}							
				}
			}	
			
			//boundaries and attractors
			for(int i=0; i<att.length; i++)
			{
				//walls-vertical boundaries
    	 		if(att[i].getX() > width - att[i].getR() && att[i].vxO() > 0)
					{	int v=(int)(ds*Math.random());	
						att[i].vxI(-v-1); 
     					v=(int)((ds)*Math.random());
						if (Math.random() >= 0.5) v=-v;
						att[i].vyI(v);
					}
				if(att[i].getX() < att[i].getR() && att[i].vxO() < 0)
					{	int v=(int)(ds*Math.random());	
						att[i].vxI(v+1); 
     					v=(int)((ds)*Math.random());
						if (Math.random() >= 0.5) v=-v;
						att[i].vyI(v);
					}
				
	  			//walls-horizontal boundaries		
				if(att[i].getY() < att[i].getR() && att[i].vyO() < 0)
    		  		{	int v=(int)((ds)*Math.random());
						att[i].vyI(v+1); 
     					v=(int)((ds)*Math.random());
						if (Math.random() >= 0.5) v=-v;
						att[i].vxI(v);
					}
				if(att[i].getY() > height - att[i].getR() && att[i].vyO() > 0)
					{	int v=(int)((ds)*Math.random());
						att[i].vyI(-v-1); 
    	 				v=(int)((ds)*Math.random());
						if (Math.random() >= 0.5) v=-v;
						att[i].vxI(v);
					}
				}

		//collisions among attractors
			for(int i=0;i<att.length; i++)
			{
				for(int k=0;k<i;k++)
				if(k!=i && overlap(att[i].getR(),att[i].getX(),att[i].getY(),att[k].getR(),att[k].getX(),att[k].getY()) )
				{	
					if(att[i].vx*att[k].vx < 0) //horizontal direction
						{	att[i].vx=-att[i].vx;
     						att[k].vx=-att[k].vx;
						}
					else if( att[i].vx >= att[k].vx ) att[i].vx=-att[i].vx;  //same direction; switch velocities
					else att[k].vx=-att[k].vx;
					
					if(att[i].vy*att[i].vy < 0) //vertical direction
						{	att[i].vy=-att[i].vy;
        					att[k].vy=-att[k].vy;
						}
					else if( att[i].vy >= att[k].vy ) att[i].vy=-att[i].vy; //same direction; switch velocities
					else att[k].vy=-att[k].vy;					}
			}	
			
		//collissions between particles and attractors
			for(int i=0;i<mass.length; i++)
			{
				for(int k=0;(k<i && k<att.length);k++)
				if(k!=i && overlap(mass[i].getR(),mass[i].getX(),mass[i].getY(),att[k].getR(),att[k].getX(),att[k].getY()) )
				{	
					mass[i].y=-(int)(height/2*Math.random());
					mass[i].x=-(int)(width*Math.random());
				}
			}	

		//move particles
		for(int i=0;i<mass.length; i++) mass[i].move();	
		//move attractors
		for(int i=0;i<att.length; i++) att[i].move();
	}
	
	private boolean overlap(int r1, int x1, int y1, int r2, int x2, int y2)
	{
		int d=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
		return (d <= (r1+r2)*(r1+r2));
	}

	private class AMouseListener implements MouseListener
   	{
     		public void mouseClicked(MouseEvent e)
     		{
       		if (timer.isRunning()) timer.stop(); else timer.start();
     		}
    		
			public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
     		public void mousePressed(MouseEvent e) {}
     		public void mouseReleased(MouseEvent e) {}
   	}

   private class AnActionListener implements ActionListener
   	{
    		public void actionPerformed(ActionEvent e)
    		{
    			move();  		
				repaint();
    		}

   	}

}

Feel free to change the number of test particles, great attractors, and dimensions of the display.

Thanks.
WCentauri is offline   Reply With Quote
Old May 27th, 2005, 2:56 AM   #2
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
duuuuuude!!! that's pretty freakin cool!!
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old May 27th, 2005, 5:51 PM   #3
Childe Roland
Newbie
 
Childe Roland's Avatar
 
Join Date: Mar 2005
Posts: 17
Rep Power: 0 Childe Roland is on a distinguished road
thats freaky to watch!
Childe Roland is offline   Reply With Quote
Old Jun 22nd, 2005, 1:00 PM   #4
WCentauri
Newbie
 
Join Date: May 2005
Posts: 2
Rep Power: 0 WCentauri is on a distinguished road
Send a message via AIM to WCentauri
Oh boy, it's been a while since I posted last. I sort of forgot about this thread. Other than showing off, I actually had an agenda.

I specifically wanted some feedback on how "realistic" or believable those interactions between the point masses and great attractors look. I think it looks choppy in some instances. If you look at the source code, you'll see that I handle the interactions with the simplest possible approximation. This approximation should work just fine if the calculation is run enough times a second.

The problem here is that the functions that draw the objects in each frame only take integers as arguments. If I could use decimals here, I could lower the velocities and make the framerate arbitrarly large and get smoother animations. Is it possible to do this with a java applet?

Part of the inspiration for this program came from the fabulous screensavers that come with Fedora Linux. I presume that those are done with Open GL. Would my animation scheme run better if it were in Open GL.
WCentauri is offline   Reply With Quote
Old Jun 26th, 2005, 11:01 PM   #5
squishiful
Programmer
 
Join Date: Jun 2005
Location: Amittyville
Posts: 60
Rep Power: 4 squishiful is on a distinguished road
mesmerizing...
__________________
I steal hippos...
squishiful is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC