Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBC] Rotate Image / Rotate Sprite

Author
Message
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 16th Nov 2008 01:42
I wrote a DLL function some time ago for rotating an image in DBC. I just never bothered writing it in BASIC because I assumed it would be way too slow, so after some talk on the DBC challenges I thought I'd convert the function over. It works ok.

The function is called rotate_img. Basically it takes 1 source image, uses another image to draw the rotation on, then uses a 3rd image as the final rotated image. This prevents the original image from being disturbed and prevents any Sprites from getting wacky as the image is being updated.

The math behind the rotation is pretty simple. Looping the x width within the y height of the target, the method calculates where on the source image a rotated pixel would come from and then copies that color to the current x,y position of the target. It works backwards in a sense. Since the source image is never disturbed, this ensures that the rotated image will not get distorted no matter how much rotation there is (sin() and cos() add up a lot of little errors over time).

The parameters are documented inside the function; but I should just mention what I refer to as matting. An image is rectanglular and when it is rotated, the spaces where the corners were have to be filled, and unless the image is resized, the corners get clipped as the image rotates. I have included 3 matting modes:

0 - maintain the original image's size. As the image is rotated, the corners will be clipped and the spaces where the corners were will be filled with black.

1 - Expanded matting. Black matting will be extended around the image increasing the overall image size but allows the image to rotate without clipping

2 - Shrink Image. The original image will be shrunk within black matting that is the actual size of the image. Rotation can occur without clipping.

Also, by default, the image rotates around it's center. You can change this using the offset parameters. Just using the offsets without changing the rotation, you can move the image around inside it's matting. I may expand the matting so that an offset image is fully visible and doesn't clip... that'll be later.

The function works for 16 bit or 32 bit images.

This example rotates a sprite. Right click to toggle transparency (of the matting) and press 0,1, or 2 to test the different matting types:



Enjoy your day.
steve paul thomas
16
Years of Service
User Offline
Joined: 4th Nov 2008
Location: United Kingdom
Posted: 16th Nov 2008 16:22 Edited at: 16th Nov 2008 16:27
@Latch

Nice code Latch Proper full image/sprite rotation in DBC! This runs very well. I see what you mean by Matting now This snippet should be good news to Irojo

Thank you very much for posting this excellent snippet.


@Everyone

Three beers for Latch!
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 16th Nov 2008 23:21
Thanks spt

I added matting option 2 as an after thought for DBC and forgot to mention that it will only work properly for square images. Options 0 and 1 work fine for any image.

Enjoy your day.
Irojo
16
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 18th Nov 2008 00:54
This is insane latch!!!




I'm trying to learn to use it, and I have a few questions.
1. With the code below, The image is moving, and I want it to stay in place. What's wrong?
2. Also, the image is getting little creases in it. Is that supposed to happen?






I download memory.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 18th Nov 2008 03:42 Edited at: 18th Nov 2008 03:48
Quote: "1. With the code below, The image is moving, and I want it to stay in place. What's wrong? "


There are a couple of reasons why you are having some trouble.
You program calls the function as:
rotate_img(1,1,1,2,1,1,angle#,1,0,-0)

First, you should use 3 different image numbers. The first image number should be the existing image you want to rotate.

The second image number should be different from the first and is used to do all of the drawing on. This can be any number you don't really care about except for the first image number or the 3rd image number.

The 3rd image number is the result of the rotation. This is the image you will use to display the rotation. If you only want the result, this is the image to keep. You can delete the other two. But if you are going to perform more rotations, do not delete the original image.

Quote: "2. Also, the image is getting little creases in it. Is that supposed to happen?"

This has a lot to do with the explanation above. You were over writing the first image so it had combinations of itself and the rotated result. That's why I wrote the function to use 3 images. It keeps things clean without too much overhead and only the "drawing" image is ever manipulated so image one stays intact, and image three is the result of the drawing image.

One other problem is that you cannot use a Float value as the counter in a For Next loop:
for angle#=0 to 360 step 1
is incorrect. The loop will loop forever and angle# will only ever be 0.

A more correct version of your code might be:




Enjoy your day.
Irojo
16
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 18th Nov 2008 04:54
That is amazing.

By amazing, I mean brilliant.

By brilliant, I mean genius.

By genius, I mean thanks a bundle.

I will cherish this function until the end of time...

Wow.


I download memory.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Nov 2008 21:15
I'm glad you like it!

Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 24th Nov 2008 05:03
And with the creation of Latch's Rotate Image function, Latch became immortalised as a God of Dark Basic
This day shall forever more be known as St. Latch's day
Hallelujah!

I've saved it and it will make my next project 100x better!

Thanks Latch, this is what DB has been crying out for.

A small program that works is better than a large one that doesn't.

DBC Challenge Rank: Rookie
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 26th Nov 2008 17:29
You're welcome.

Just a note: using this in real time for small images may be passable, but the larger the image gets, the more individual pixels have to be "moved" - so the slower the performance. For larger images it is best to store each rotation as it's own image then "page flip" through the images. Here's two examples. The first is a full screen real time rotation. The second captures the screen rotations then "page flips" through them. While the second one takes a minute to set up, the result is much faster than the real time version:

Example 1:


Example 2:


Enjoy your day.

Login to post a reply

Server time is: 2024-11-22 07:27:25
Your offset time is: 2024-11-22 07:27:25