Game Screen "Shake" and Gradient Paint in a Directed Cone

Alright, I have two questions and I didn’t want to clog up the forums with topics, so I thought I’d put them in a single post.

1.Making a Game Screen “Shake”

Right, so in the game “Droid Assault” which I was introduced to by these forums, sometimes, when you do some electricity thing, the game screen kind of shakes back and forth. At first I thought, "Hey all they did was take the game, pause it, take the current screen and put it into a buffered image and do some jiggery hokery with sin and such… But I am not quite sure now. Does anybody know how to achieve this?

  1. Making a cone with a gradient

Again with Droid Assault, I saw the lights that follow the player and fade from red to transparent in a conical shape. I thought they may have used gradient paint, but I am not experienced with that at and after some research I still could not think of a way. As you can probably see I am a little bit of a novice. Suggestions? Thanks -cMp

  1. Many ways you can do this. Probably the easiest is translating by a vector whic is in turn moving in random directions.

  2. Shaders.

I simply translate the modelview matrix by a random amount before drawing the game:


void render() {
	float yOffset = 0.0f;
	if (shakeTick > 0) {
		shakeTick--;
		yOffset += Util.random(-shakeTick, shakeTick);
	}
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.0f, yOffset, depth);
	
	// draw everything else
}

The cones of light are just big sprites.

Cas :slight_smile:

Dang sorry guys, I meant to say I am just using the Java Graphics library. Sorry bout the miscommunication. Any ways to do this in plain java?

How about:



//You untranslated Graphics object.
Graphics g = ...;

if(shakeTick > 0){
   shakeTick -= delta;
   g.translate(/*random values*/);
}

//Draw everything else...

This could work.
Im always using OpenGL since it’s easier to do graphical effects with it.

  • Longor1996

That’d work.

Cas :slight_smile:

Works great! I dont know why I didnt think of that before… And when you say the cones of light are big sprites, I automatically think, “Rotation? How???”. I have used (Graphics2D Object).setTransform() before, but I really really dislike it because it confuses me a ton. Is there a way to avoid this and just use something else? Thanks cMp

Java2d rotation is silly easy.


Graphics2D g2d = (Graphics2D)g.create();
g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));

rotation.x is your rotation in degrees 0-360

loc.x and loc.y are your coordinates on the screen.

Do this before drawing.

Make sure to call g.dispose() when you are done drawing.

Well I have seen that, but is the x and y coordinates the x and y you are giving to g2d.drawImage(), or is it the anchor coordinate, or something else?

http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/AffineTransform.html

It depends on how you want to rotate. In most cases it will be the center point of your sprite. If you draw with the starting point being in the top left corner, then you need to add half your sprites width to the x and half of the sprites height to the y.

Alright, I think I have it all working! I’m gonna update my post with my game in it soon with some great improvements and new features :smiley: