A little bit of math - Rotation

Hey guys,

I never listened to our maths teacher back in school time, now I could kick myself in the a** :(.

I got a laserbeam and at the end a burst (in this case a dot). This laserbeam is rotating from the center to all the edges and so on.
How can I calculate the new coordinates for the end of the beam, where should be displayed an image of a typical laser burst.

My first attempt (thought) was to use some Math.sin and cos. But I really do not know how to use them properly. Wikipedia couldn’t help me neither, it’s not game dev related enough. I hope you guys are patient and could help me with this, hopefully trivial, question.

Thanks in advance. :slight_smile:
Kronos

I was to quick writing this thread :frowning:

I added a g.resetTransform(); before the burst image even could rotate…
however… is there an idiot secure mini tutorial for wanne-be game developers regarding cos(), sin() and PI, which you would recommend?

Here’s something I just whipped up.


public float angle;
public void update()
{
	angle += 0.5; //increase the degrees by 0.5 each update
	if (angle >= 360)
	{
		angle = 0;
	}
}

public void draw(Graphics g)
{
	//if the width is longer than the height, use this, otherwise switch to
	//screenHeight and centerY
	int radiusOfScreen = screenWidth - centerX;

	//convert degrees into radians to use with cos and sin
	int endX = (int)(centerX + radiusOfScreen * Math.cos(Math.toRadians(angle)));
	int endY = (int)(centerY + radiusOfScreen * Math.sin(Math.toRadians(angle)));
	g.drawLine(centerX, centerY, endX, endY)
}

Thanks vbrain.

I came up with a new problem. How can I manage stop increasing my laser’s beam width when it collidates with the frame bounds (or objetcs flying around). I do not know how to calculate the ending point of my Rectangle, when it is rotated. The beam currently goes endlessly into the wide nothing (out of frame)…

:o

edit:

I think I made it. It looks good so far.

endPos.set((float)(x + width * Math.cos(Math.toRadians(rotation))), (float)(y + width * Math.sin(Math.toRadians(rotation))));

width is the radius. This gives me a Point of the “circle” -> my rotation. for those who had the same problem, or will have the same problem in the future.

I am a one man show :smiley: