Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Animation basics  (Read 825 times)
0 Members and 1 Guest are viewing this topic.
Offline trajedebano

Junior Newbie





« Posted 2006-02-10 16:43:13 »

Okay, so I've ventured into my first bit of java programming by making a Tetris clone.

One of the things I wanted to do is make it so the animation was very smooth to make it appear as though the pieces were sliding instead of moving down the grid block by block. I did accomplish this, by running a general animation thread that processes animations for the pieces.

You can take a look at the applet here...
username: guest
password: guest
http://www.grnade.com/tetris/

I've attached what my animation thread looks like if anyone wants to see what I mean.


Now heres the thing. I'm ready to add more animation to this to spice it up -- you know, effects when you remove a line, perhaps making animations in the background, etc. The thing is my animation thread is going to get cluttered, and it doesn't really make sense to have just one thread controlling all animations. It's made timing things a real trick.

I know there is a better way to do this, I'm just looking for some sort of enlightenment here.
I tried giving each animation its own thread object by doing something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
    
Thread animateDrop = new Thread() {
        public void run() {
      if (yOffset>=20) {
         translate(0,1);
         yOffset = 0;
      } else
         yOffset++;
      try {
         sleep(speed/20);
      } catch (InterruptedException e) { }
        }
    };
    animateDrop.start();


The problem with that is objects with threads like that can't interact with my yOffset variable(the variable that is added in my paint method to give the 'sliding' appearance)

So I guess my question is, whats the best way to tackle 2d animation where you could quite possibly have 6 or more animations going on at once? Is there a general consensus here? If anyone even has some good animation programming links, I'd love to see them.
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2006-02-10 17:09:32 »

First rule of gaming: Always keep your rendering in one thread.
Second rule of gaming: See rule #1

What you want to do is build objects that will render your special effects. For example, if you have an explosion to render, make a class like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
public class Explosion
{
  private Image[] animation;
  private int x;
  private int y;
  private int counter = 0;

  public Explosion(Image[] animation, int x, int y)
  {
    this.animation = animation;
    this.x = x;
    thix.y = y;
  }

  public void render(Graphics g)
  {
    if(counter >= animation.length) return;

    g.drawImage(animation[counter], x, y, null);
    counter++;
  }

  public boolean isDone()
  {
    return (counter >= animation.length);
  }
}


To render the explosion, you'd just add it to an array list that you'll loop through in your main rendering loop. When the animation claims that it's done, you simply remove it from the list and allow it to be garbage collected.

The above example is, of course, very simplistic. A real-life example would probably include more counters (perhaps even particles all over creation) and a more complex positioning model, but  the basic concept is the same.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline trajedebano

Junior Newbie





« Reply #2 - Posted 2006-02-10 17:14:30 »

Interesting...

you have it so the object itself draws to the graphic. i was painting the objects in the paint method by just painting with whatever the values are at the moment paint() is called.

just out of curiousity, what is the reasoning behind keeping all rendering to one thread?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2006-02-10 17:44:41 »

you have it so the object itself draws to the graphic. i was painting the objects in the paint method by just painting with whatever the values are at the moment paint() is called.

My method isn't any different, it just unclutters your rendering thread. My rendering thread would look something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
while(true)
{
    Graphics g = bufferStrategy.getDrawGraphics();

     //Draw background, and foreground

    for(int i=0; i<specialEffects.size(); i++)
    {
        SpecialEffect effect = (SpecialEffect)specialEffects.get(i);

        effect.render(g);

        if(effect.isDone())
        {
            specialEffects.remove(i);
            i++;
        }
    }

    g.dispose();
    bufferStrategy.show();
}


Quote
just out of curiousity, what is the reasoning behind keeping all rendering to one thread?

Simplicity and synchronization. When you go multithreaded, you never quite know who's changing data or writing to the backbuffer at a given time. This can result in a lot of difficult-to-debug situations where graphics will flicker, jump, tear, etc. Sometimes it can even screw up your logic causing the wrong thing to happen at the wrong time.

As luck would have it, it's far simpler to ignore multithreading and do one thing after another. You've basically got 1/60th of a second to render everything you can to the backbuffer before the VSync happens. Once the VSync happens, your backbuffer will become the frontbuffer, and you'll begin rendering the next frame. Since the point is to build the next frame of animation, there's no real need to make anything happen concurrently.

As long as you keep things predictable, it's very easy to write a game.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Jeff

JGO Coder




Got any cats?


« Reply #4 - Posted 2006-02-11 02:42:26 »

In general, action games do *everything* on a single thread.

(1) On a  single core/CPU system its more efficient
(2) Games are step-wise time-based simulations.  You want a single timebase to calculate input and reuslts in.  In non-techie, that translates to "you want to worry about 1 frame at a time and get each frame exactly right."

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!