Advertisement

[java] Java 'machine'/'lego' game

Started by August 18, 2000 10:03 PM
2 comments, last by HippyNerd 24 years, 4 months ago
Greetings I''m currently learning java, at a fairly fast pace, and for a programming project for uni i''ve decided to make a kinda ''physics'' game/simulation thing. I''m not too worried about accuracy, etc, but I want to make it functional. Originally i was thinking about something along the lines of a 2D lego technic simulation. Since then i''ve decided this''ll probably be too hard. Right now i''m thinking of something maybe more along the lines of ''the incredible machine'' (from about 6 yrs ago). Anyway right now my 2 biggest problems are: 1) collision detection -> MAJOR problem. All i know now is how to detect if something collides with a bounding box of an object. However right now, all i want to do, is have a ball that bounces around the screen in a physicly correct manner, i.e it hits things at angles and bounces off at the correct angle etc. But is there any efficient way of doing this? Is there any fast java function that returns whether or not a point lies inside an object that isn''t necessarily a rectangle who''s sides are parallel to the applet window. 2) Redrawing stuff...say I have 3 balls bouncing around, but about 15 things the balls can bounce off. Only the 3 balls change position every tick. Is there anyway that I can speed my program up, not redrawing every single object on the screen. I''ve heard of cliping boxes but i havn''t read up enough on them to know if they''d be suitable for moving sprites. I know one thing I could possibly do is just make a background image with the objects predrawn, but i''d like to let the user place their own objects on the screen. 3)User interface -> I don''t have too much idea how how i''m going to go about it at this point. I was thinking of some drag''n''drop thing where the user picks up balls/ramps etc from a side bar, and drags them onto the playfield and drops them. Since this is more of a ''simulation'' once all the objects are positioned and the user presses "GO" then no more interaction is necessary. Anyways, I was just wondering what I''d need to do this, i''m just confused over AWT and SWING. Anyway thanks to anyone who can answer some of these badly worded questions, and i''ll probably come back here pretty soon and ask more stupid questoins.
Hi,
Let's see if I can help you out a bit...
* pseudo-ish code for the code frags..

1.) For the first problem I suggest using the Polygon class (in the java.awt package). Here you can specify a shape like this :

2.) For the second problem I suggest you using double buffering
and prepare another image for the background - (not fast but your animations will look smooth and that's the point)

       #1     // where xpoints and ypoints are array of ints Polygon p = new Polygon(xpoints[],ypoints[],numPoints)// then to see if a point is inside your polygonif(p.contains(x,y)) {  // do something...}//or if you want to see if a rectangle collided/insideif(p.intersects(x,y,width,height) {  // do something..}#2// when the user is finised placing the objects// set up the back image that way you don't have draw // each object just one big one ...public Image setUPBackgroundImage() {   Image back = createImage(width,height);   Graphics g2 = back.getGraphics();      for(int i = 0; i < numObjects; i++) {      g2.drawImage(obj,x,y,null);    }      return back;}.....// then call the above function when they've finished like// so backImg = setUPBackgroundImage() ;........// then in your paint method or updatepublic void paint(Graphics g) {  Image buffer = createImage(width,height);   Graphics offScreen = buffer.getGraphics();  offScreen.drawImage(backImg,x,y,null);  for(int i = 0; i < numBalls; i++) {    offScreen.drawImage(ball,x,y,null);  }  g.drawImage(buffer,0,0,null);}    


3.) For your third question, it really matters what your making an applet or an application. If you're making an applet you should use AWT, because swing is not supported (well) on the browers. For applications use SWING, because it's a lightweight interface that's easy to use... For documentation on SWING and AWT go here.

So I hope I helped you a little () with your problems.
Remember their are more ways to do the stuff the I've shown, so if you don't like it,I'm sure someone will correct me on this forum.


=============================
silly programmer vb is for kids.
============================
www.thejpsystem.com

Edited by - loserkid on August 19, 2000 3:45:24 PM

Edited by - loserkid on August 19, 2000 3:48:26 PM
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
Advertisement
That polygon class helps heaps. Thanks for the help looser kid. I"ve got a lot to learn about java, but i find learning this way is quite fun

I kind of like the idea of an applicatoin more than an applet. Also, how soon before we can expect compilers for java applicatoins...

I odn''t really know what i''m saying.. all i know is that it was quite depressing watching my applet in netscape (i''ve done absolutely no optimisation though). Then i ran my applet in IE5 with the JIT enabled...and it was sweet.
In answer to question 2, as well as using buffering, overload the public void repaint().
"We who cut mere stones must always be envisioning cathedrals"

This topic is closed to new replies.

Advertisement