2

While working on Projectiles I thought that it would be a good idea to rotate the sprite as well, to make it look nicer.

I am currently using a 1-Dimensional Array, and the sprite's width and height can and will vary, so it makes it a bit more difficult for me to figure out on how to do this correctly.

I will be honest and straight out say it: I have absolutely no idea on how to do this. There have been a few searches that I have done to try to find some stuff, and there were some things out there, but the best I found was this:

DreamInCode ~ Rotating a 1-dimensional Array of Pixels

This method works fine, but only for square Sprites. I would also like to apply this for non-square (rectangular) Sprites. How could I set it up so that rectangular sprites can be rotated?

Currently, I'm attempting to make a laser, and it would look much better if it didn't only go along a vertical or horizontal axis.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
CoderMusgrove
  • 604
  • 8
  • 18
  • Why aren't you using one of the many game engines for Java? Or if you are, which one? Why write the sprite rotation code by hand? – jefflunt Jun 20 '14 at 21:08
  • Also, if you're trying to make a laser, why not just draw a line with a certain line thickness, rather than a sprite? – jefflunt Jun 20 '14 at 21:09
  • @jefflunt I'm currently writing the engine from scratch because I would like to have a better grasp, and I feel that I would get more accomplished, although I plan onto moving into the game engines soon. As well, I'm not drawing a line because I am manipulating an array. – CoderMusgrove Jun 20 '14 at 21:11
  • 1
    Well, that's a nice thought, and I understand because I've been there, but there are better ways to achieve the goal of having a good grasp on things, like reading, working on, and/or contributing to [an existing engine](http://stackoverflow.com/questions/8363606/slick2d-vs-straight-lwjgl/8365799#8365799). Unless you think you'd like to write basic graphics code for a living, this is a bit like re-discovering addition, rather than taking it for granted from math class. There's something to be learned, yes, but you're sort of in the weeds when maybe what you'd rather be doing is making a game. – jefflunt Jun 20 '14 at 21:16
  • I believe your right on this, and thank you for that well written response. I suppose it would be better than what I'm trying to accomplish. – CoderMusgrove Jun 20 '14 at 21:22
  • If I hadn't wasted years re-writing already solved algorithmic problems, then I'd probably have a different perspective. I admire when people want to know for themselves how something works, but if you spend all your time writing engines then when you finish you'll have an engine but you still won't have a game. My advice is to work on the game instead and just eat the learning curve of existing, performant, reliable tools that already exist. – jefflunt Jun 20 '14 at 21:33
  • As an aspiring hacker, I respectfully disagree with this. The OP should at least write ONE engine. The experience you've gained that YOU (@jefflunt) are subconsciously taking advantage of is the experience that allows you to determine if a bug is from your use of a lib/engine or from the engine itself. If you've written engines, you by sheer virtue of having done more than is immediately necessary, have an advantage over all other library consumers. – avgvstvs Nov 20 '14 at 14:26
  • I still wish I'd contributed to an existing engine rather than writing bare-metal rendering code. I would have 10 games done by now. Instead, I'm a web developer, and I've never written my own web framework, or web server, or DB server...but I understand each of their roles, how they work, and how they interact with one another. If the goal is to create a **game** then the majority of effort, IMO, should be spent on the game, not the tools. How many carpenters do you know who have created their own hammers? That would be utterly nuts. – jefflunt Nov 20 '14 at 20:49

2 Answers2

0

You need to recalculate the coordinate points of your image (take a look here). You've to do a matrix product of every point of your sprite (x, y) for the rotation matrix, to get the new point in the space x' and y'. You can assume that the bottom left (or the bottom up, depends on your system coordinate orientation) of your sprite is at (x,y) = (0,0)

And you should recalculate the color too (because if you have a pure red pixel surrounded by blue pixel at (x,y)=(10,5) when you rotate it can move for example to (x, y)=(8.33, 7.1) that it's not a real pixel position because pixel haven't float coordinate. So the pixel at real position (x, y)=(8, 7) will be not anymore pure red, but a red with a small percentage of blue)... but one thing for time.

Accollativo
  • 1,537
  • 4
  • 32
  • 56
0

It's easier than you think: you only have to copy the original rectangular sprites centered into bigger square ones with transparent background. .png files have that option and I think you may use them.

Eduardo Soriano
  • 184
  • 1
  • 12