Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Writing 2D Games with Java  (Read 3354 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: August 14, 2007, 07:03:34 AM »

Expanding on your knowledge of sprites, this chapter focuses on developing a Java two-dimensional game engine. Animating game objects is not all that different from animating sprite characters. A few advanced motion tricks are needed, but the transition is not too difficult. The game engine will be used as the basis for the arcade standard, Asteroids. In practice, any 2D action game could be written using the techniques you'll learn in this chapter.
2D Game Basics

Two-dimensional games require a technique embodied in early Atari personal computers, namely, player-missile graphics. Essentially, all screen objects need to know their position and if they are colliding with another entity. Beyond this, objects need to be able to move around and to rotate.

For simplicity, this chapter exploits a class in the AWT for representing objects on the screen. The Polygon class is a wonderful class for manipulating a variety of two-dimensional shapes. Polygon objects can have virtually an unlimited number of vertices and so can represent everything from simple squares to a complex space station.

Unfortunately, the Polygon class does not contain such 2D basics as rotation, scaling, and translation. The Polygon class is used only for displaying an object; a separate class is used to represent and track objects around the screen. In order to write this class, some two-dimensional basics must first be covered.
Scaling an Object

Given an ordered set of two-dimensional points, how can it be grown or shrunk? To start with, define how your points are stored. Because the Polygon class is used for display, it makes sense to store two separate arrays of points, one for the x coordinate and one for the y coordinate. To scale such a set, simply multiply the scaling factor by each vertex. The following code snippet makes a given polygon twice as large:

Code
GeSHi (java):
  1.    for ( int v = 0; v < numVertices; v++ )
  2.    {
  3.        xpoints[v] *= 2;
  4.        ypoints[v] *= 2;
  5.    }
Created by GeSHI 1.0.7.20

This code works to both expand (scale > 1) and contract (scale < 1) any polygon. To scale in only the x or y direction, multiply only one of the coordinate arrays.
Translating an Object

Object translation has nothing to do with foreign languages. Translation refers to moving an object without changing its orientation or size. The operation is identical to scaling, except instead of multiplication, addition (or subtraction) is used. The following code snippet translates an object in x-y space:

Code
GeSHi (java):
  1.    for ( int v = 0; v < numVertices; v++ )
  2.    {
  3.        xpoints[v] += 2;
  4.        ypoints[v] += 2;
  5.    }
Created by GeSHI 1.0.7.20

Nothing precludes you from using different additives for the x and y coordinates. In fact, this is very common. Usually, the previous code is actually written as follows:

Code
GeSHi (java):
  1.    for ( int v = 0; v < numVertices; v++ )
  2.    {
  3.        xpoints[v] += xamount;
  4.        ypoints[v] += yamount;
  5.    }
Created by GeSHI 1.0.7.20

For more info how to write 2d games with java read here: http://docs.rinet.ru/JaTricks/ch14.htm#DGameBasics
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here