• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Rotate at a point

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1P0jahn  Members

Posted 10 April 2014 - 12:41 PM

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);


### #2DiegoSLTS  Members

Posted 10 April 2014 - 04:05 PM

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.

### #3TheChubu  Members

Posted 10 April 2014 - 07:02 PM

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

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

### #4P0jahn  Members

Posted 11 April 2014 - 10:39 AM

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.

http://youtu.be/wsZdjFs9iKc

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.

### #5DiegoSLTS  Members

Posted 11 April 2014 - 11:14 AM

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.

http://youtu.be/wsZdjFs9iKc

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, 11 April 2014 - 11:18 AM.

### #6P0jahn  Members

Posted 13 April 2014 - 06:59 AM

Thank you very much. Works perfect

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

Edited by P0jahn, 13 April 2014 - 07:00 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.