• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
P0jahn

Rotate at a point

5 posts in this topic

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:

 

img.png

 

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

Share this post


Link to post
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.

0

Share this post


Link to post
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

0

Share this post


Link to post
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.

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.

0

Share this post


Link to post
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.

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
0

Share this post


Link to post
Share on other sites

Thank you very much. Works perfect smile.png

 

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

Edited by P0jahn
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0