It looks like you're new here. If you want to get involved, click one of these buttons!
I have a sprite that needs to be rotated at a custom angle. I know there is a math.rotate() function, but can this function be transferred to a sprite or image? I need to be able to individually rotate many sprites, all at custom rotations. Finding the right rotation value will be difficult in it self, but I want to know how rotating sprites works before I get started.
Comments
Hello @edat44. One thing you can do, is illustrated by the code below (updated: comments added):
. @edat44 and for those interested, another way is to use mesh() instead of sprites.
Not tested but this should work.
Cheers
Thanks @mpilgrem. I was even able to use code with the new parameter and tween features in the newest Codea update to smoothly animate the sprite.
Sorry to necro this thread, but does anyone have any thoughts on the differences in performance between these two methods?
Edit: I've just tested them both, and the Sprite version is faster - perhaps due to the Pic version requiring extra parameters, even if you don't want to scale.
Generally, meshes outperform sprites, not least because sprites are actually drawn using meshes, behind the scenes. The difference may not be noticeable unless you have a lot of images.
If you're going to do any serious graphics, though, I would focus on meshes.
That's really weird. Using close-to-identical code, I can get 400-odd sprites, but only 300-or-so meshes! I'm a bit baffled.
@Madrayken - maybe you should post your code
@Madrayken, if you're creating meshes every frame and drawing loads of different meshes then it can get slow, I find using addRect and multiple rectangles much faster than 100 meshes with 1 rectangle or a sprite. Creating rectangles for one mesh every frame is a lot faster than creating a whole new mesh
Clearly, I need to understand meshes better. :-) Thanks.
Posting code below. No idea why the formatting has gone so horribly wrong...
@Madrayken - I've fixed your code formatting above. The trick is to put three ~ on the line before your code, and three more on the line under your code.
You can make the sprites faster still by reading the image into a variable in setup, and spriting that in draw, ie in setup
img = readImage("Dropbox:Head")
and in draw
sprite(img, 0, 0)
I'm not sure why the meshes are slower at this stage
This code runs meshes really fast, by putting all the images in a single mesh (because they all use the same texture image). This is much faster because each mesh has a setup process.
It also explains why the sprites were faster, because Codea batches sprites behind the scenes - I'm guessing it creates a single mesh from all of them (if you weren't aware, sprites are actually meshes as well).
@Ignatz - I have used self.w, self.h = spriteSize (img) instead of scr. FPS = 60
Good job.
Thanks sooooo much, Ignatz. I was tearing out my hair over both those issues!
As an aside - is this 'make them all one mesh' technique a good one for a simple tile-based background, or do you have a better suggestion?
If the background isn't ever going to change, just draw it all onto a single image at the beginning with setContext, then sprite that in draw.
If is a dynamic background and you do have to draw it fully all the time, then yes, a single mesh is quicker than a whole lot of meshes. However, if each tile has its own image, you can't combine them in one mesh (because a mesh can only have one texture image) unless you cram all the tiles onto one image and select the right piece to use for each tile.
There was quite a lot of discussion on this for a game called RPGMaker - it might be worth searching the forum for that.
Here's another example of rotating sprites. Let it run for at least 2 minutes.
Here's the above program, but the rotation angle is based on where you slide your finger up or down the screen.
EDIT: Added math.floor()