This topic is now archived and is closed to further replies.

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

This topic is 6358 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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)

// where xpoints and ypoints are array of ints

Polygon p = new Polygon(xpoints[],ypoints[],numPoints)

// then to see if a point is inside your polygon

if(p.contains(x,y)) {
// do something...


//or if you want to see if a rectangle collided/inside

if(p.intersects(x,y,width,height) {
// do something..


// 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++) {
return back;
// then call the above function when they've finished like

// so backImg = setUPBackgroundImage() ;
// then in your paint method or update

public void paint(Graphics g) {
Image buffer = createImage(width,height);
Graphics offScreen = buffer.getGraphics();

for(int i = 0; i < numBalls; i++) {

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.

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

Edited by - loserkid on August 19, 2000 3:48:26 PM

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
In answer to question 2, as well as using buffering, overload the public void repaint().

Share this post

Link to post
Share on other sites