# Rotate at a point

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

## Recommended Posts

I am in process of writing my own laser drawing function and I have ran into a math problem.

Have a look at the following image:

The laser is set to fire at a certain point(the top-left corner of the red guy), which it actually does. The top-left, or the bottom-left(not sure which one) corner of the laser beam is exactly at the target coordinate.

What I want to do is to have the lasers middle point at the target point, not the corner.

Here is my code, I am using the following method to draw the laser:

SpriteBatch.draw(TextureRegion region,
float x,
float y,
float originX,
float originY,
float width,
float height,
float scaleX,
float scaleY,
float rotation)



And here is my code:

float angle = MathStuff.getAngle(srcX, srcY, destX, destY); //Calculate the angle between the source and destination point.

batch.draw(beamImage,
srcX,
srcY,
0,
0,
MathStuff.distance(srcX, srcY, destX, destY),
beamImage.getHeight(),
1,
1,
angle);


##### Share on other sites

Your laser is a single image? Does it spin from the center of one side or from the top-left corner?

If that's the case, I think that you're making the rotation right (there's nothing wrong with the code you posted), but you're drawing the laser wrong. You might want to translate the image so that the center of the origin side is at (originX,originY) position.

Also, I think you might want to point to the center of the meat boy instead of the corner, it makes things easier. Well, generally, it makes it easier to work with the center of things, and even more when rotations are involved.

##### Share on other sites

Also, consider using some structure for your coordinates. Like "Point2D" with 'x' and 'y' fields. Makes using the functions easier since you'd call say, getAngle with getAngle(aPoint,bPoint) instead of having tons of x,y parameters for each point involved.

Just try to be sensible when allocating new objects since its easy to write a function that needs, say, 3 additional Point objects and allocates 3 of them each time its called. Makes the managed runtime run more the garbage collector and generally contributes to giving managed languages a bad name :P

##### Share on other sites
Your laser is a single image?

Its actually two images. The first part is at the beginning, the part that grows. I did not include that in the post because I dont want to show unnecessary code.

I am also going to remove it.

Does it spin from the center of one side or from the top-left corner?

Do you mean what point it rotates around? Since origin X and Y is 0, it should be the top-left corner.

Here is a youtube video I just made that should explain it better:

(Note that the black dot is srcX srcY point.

If that's the case, I think that you're making the rotation right (there's nothing wrong with the code you posted), but you're drawing the laser wrong. You might want to translate the image so that the center of the origin side is at (originX,originY) position.

If I understand you correctly, I should set originY to the laser height / 2?

Also, I think you might want to point to the center of the meat boy instead of the corner, it makes things easier. Well, generally, it makes it easier to work with the center of things, and even more when rotations are involved.

Absolutely. I am only doing this for debugging.

##### Share on other sites

Your laser is a single image?

Its actually two images. The first part is at the beginning, the part that grows. I did not include that in the post because I dont want to show unnecessary code.

I am also going to remove it.

I asked because maybe you where drawing the beam with lines and some effects instead of images, just wanted to be sure.

Does it spin from the center of one side or from the top-left corner?

Do you mean what point it rotates around? Since origin X and Y is 0, it should be the top-left corner.

Here is a youtube video I just made that should explain it better:

(Note that the black dot is srcX srcY point.

Thanks, that's what I thought.

If that's the case, I think that you're making the rotation right (there's nothing wrong with the code you posted), but you're drawing the laser wrong. You might want to translate the image so that the center of the origin side is at (originX,originY) position.

If I understand you correctly, I should set originY to the laser height / 2?

Not exactly, you need to make sure that the center of the origin side of the image is at the the originX and originY of the bean, so you need to draw it on another point. That point must be computed since it depends on the angle, if the beam points down the top-left corner of the beam image is translated height/2 in the X axis. You can use sin and cos functions to know how much to translate in each direction, dX = height/2*cos(angle + 90°) and dY = height/2*sin(angle + 90°), then you call:

batch.draw(beamImage,
srcX + dX,
srcY + dY,
0,
0,
MathStuff.distance(srcX, srcY, destX, destY),
beamImage.getHeight(),
1,
1,
angle);


Edit: Fixed an angle problem, you need to add 90° to the angle since you want dY = height/2 when your beam is horizontal. Also, if your Y axis starts from the top of the screen you should do dY = - height/2*sin(angle + 90°)

Edited by DiegoSLTS

##### Share on other sites

Thank you very much. Works perfect

The lasers middle is also at the target coordinate, which is a desired effect :)

Edited by P0jahn

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 20
• 20
• 11
• 9