Link to home
Start Free TrialLog in
Avatar of hansell
hansell

asked on

Rotate sprite in mcga

If I'm currently in Mcga mode, and have procedures to create and draw sprites from the screen and onto the screen, how do i go about rotating a sprite a have?  I don't know how to rotate anything really ):  If it also falls along the same lines, how would I scale something also?  As always, source is helpful!  Thanks!

-Brian
Avatar of scrapdog
scrapdog
Flag of United States of America image

Can I see the data structure (object, record, etc.) you are using to store your sprite?

The way I would go about doing it is mapping it to another sprite.  If you show me the data structure, I can show you how to scale and rotate it.

Avatar of Motaz
Motaz

Matrix multiplication is used to scale and rotate images.

Motaz
ASKER CERTIFIED SOLUTION
Avatar of scrapdog
scrapdog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello ScrapDog.... I like your sprite rotating program, and was wondering if you could teach me how to rotate graphics... What are some of the principles of rotating images as a whole and working with graphics.???

10x

Regards,
Viktor Ivanov
Viktor:

It is all about putting images through a transformation function.  A transformation function can rotate, scale, invert, or do anything that changes the actual contents of the images.

X and Y (where to extract the pixel) are dependent on a function of the X and Y of the new image.

For example, you would scan the coordinates of each pixel of the new image, and plug them into this function.  From the function you will get the coordinates of the old image.  So you take the pixel that is located at these coordinates in the old image, and plot it onto the coordinates of the new image.

ALL pixels in the new image will be defined, BUT not all pixels in the new image will necessarily make it to the old image.

For each coordinate of the old image, the new coordinate can be found by using this formula:

OldImageX = NewImageX * cos(theta) - NewImageY * sin(theta)
OldImageY = NewImageX * sin(theta) + NewImageY * cos(theta)

(theta is angle in radians)

Let me try to clarify it.  You would use a loop for NewImageX and NewImageY (making it an independent variable), and for each of these get the OldImageX and OldImageY, extract the pixel from it, and plot it at NewImageX and NewImageY (does this make any sense?)

Scale works in almost the same way.  If you look at my code above you will see that it divides by the scale to get the old image coordinates.

I don't know what else I can tell you...any specific questions?


scrappy

Hello scrapdog... 10x for clarifying things...

I have never worked with graphics and stuff and I'm planning to do so in the near future... I mean I'd have to find some interesting documents on how to work with the graphics and so on.... I have a few questions I'd like to ask you, though... The thing I really don't understand is how to rotate the pixel from one place to another, that is how to use cos() sin() what does it do to the pixel that is taken???

lets say we have a pixel  like so..

var
  Pix : Byte;
begin
  //What code do I need to rotate this pixel from one place to another??? take a look at scheme below
end;

This is like a sprite...

o -> pixel to move            c -> move pixel to here
o.......                                  ..........
........                                 ..........
........                                 ........c

what the code is going to be to move the pixel "o" to the same image but at the place of pixel "c"???

I hope you understand what I mean.,,,

Regards,
Viktor Ivanov

You don't do anything with the pixel itself.  You only operate on it coordinates.

I will use the Cartesian coordinate system to simplify my explanation a bit.  Hopefully you are familiar with Cartesian coordinates.

First, lets say you are rotating 35 degrees.

In the destination image, let's say you want to get the pixel for (3,2).

.|...
.|... <-find pixel to place here
.|...
-------
.|...
.|...
.|...

Remember the formula:
OldImageX = NewImageX * cos(theta) - NewImageY * sin(theta)
OldImageY = NewImageX * sin(theta) + NewImageY * cos(theta)

OldImageX = 3 * cos(35 deg) - 2 * sin(35 deg)

3 * 0.819 - 2 * 0.574 = 2.457 - 1.148 = 1.309

OldImageY = 3 * sin(35 deg) + 2 * cos(35 deg)

3 * 0.574 + 2 * 0.819 = 1.722 + 1.638 = 3.36

So, the pixel will be taken from (1.309, 3.36) in the source image.

We have to round this off to (1,3).

.|*..
.|...
.|...
-------
.|...
.|...
.|...

The * is located at (1,3) in the source image.  So we take the color of the pixel that is located there, and place it in the destination image (the coordinates were (3,2)).

.|...
.|..* We now have the pixel from the source image
.|...
-------
.|...
.|...
.|...


That was only for one pixel.  We have to do it for the whole destination image.  So we do this one coordinate pair at a time until the destination image is full.

Just remember that the destination "extracts" from source, rather than source "copies" to destination.

Any clearer now?


scrappy
Yup, it is getting clearer.... I'm pretty good in math but I don't know how to use that in programming..... How do you remmember all this stuff about cos() sin() tan() where to put it?? How do you know what the cos() for example or Sin() is gonna do to a coordinate or a value...or whatever...???  To clarify things, here is a summary of what I just said,,, How do you know what cos() is going to do for example to the coordinate (3,2) ... Man I want to learn these graphics so much but it seems to be difficult until you get the trick... 10x.. Would you please show me in a source code just a simple procedure that rotates an image as parameters to use the following...

procedure Rotate(SourceImg, DestImg : TImage; Angle : real);
begin
//the code??? . o O
end;

Can you show me an example program that draws a triangle...doesn't matter how big or what coordinates it has.... Then after it draws the original how it would mirror the triangle so the same triangle is drawn the opposite way..
    .
 .     .
........
........
 .     .
    .

Something like this thing above???

10x a lot for your time!

btw- How long did it take you to learn all this stuff???

Regards,
Viktor Ivanov
Viktor:

1. Check out the thread on the Delphi board (How to rotate an image).  This thread is a few weeks old, so you might have to hunt a bit.  I just posted how to rotate one rectangle on a canvas to another rectangle in another canvas.  You could use plug Image1.Canvas into it if you wanted.

2. As much as I'd love to help you out with graphics programming, it is a huge domain and takes a lot of practice, so I don't know if I could teach you very efficiently in writing.  But if you ask me one question at a time I can try to answer them.  As far as your triangle question:

T1, T2, T3 are points (in Cartesian coordinates)

Line(T1.x, T1.y, T2.x, T2.y);
Line(T2.x, T2.y, T3.x, T3.y);
Line(T3.x, T3.y, T1.x, T1.y);

of course, Line is pseudocode.  (In Delphi you could use LineTo and MoveTo, etc.)

To mirror it, you could use the opposites of the points.

Line(T1.x, T1.y, T2.x, T2.y);
Line(T2.x, T2.y, -T3.x, -T3.y);
Line(-T3.x, -T3.y, T1.x, T1.y);

etc.

3.  To explain how Cos and Sin work would literally take hundreds of pages.  I would suggest a trigonometry course :)

4.  I don't know exactly how long it took to learn.  I just assimilated a little bit of it at a time over the last few years...I suppose I started learning this stuff the very first time I used Terrapin Logo (13 years ago).


scrap-doggy-dog


Wow It seems like I need to eat a lot of food until I learn this stuff.... How old are you now??? I'm 16... I'm taking trigonometry now....so maybe I'd be able to learn some stuff in this year's math course... I knew how to do the tringle with LineTo() that was actually an example....but what I meant is do it pixel by pixel....

10x

Regards,
Viktor Ivanov
I assume you mean *rotate* it pixel by pixel, because drawing it pixel by pixel would be very impractical :)

To flip it around, just pass it to the rotate function and rotate it 180 degrees.  Try my function I wrote in this thread (above).  Make a sprite containing a triangle, then do a trace to see how it works.

Scrapdog (22)


ThanX I'll try that... I started programming three months ago so I don't feel like I'm much behind.... 10x for your help :-)

Regards,
Viktor Ivanov
zup Viktor:

Just a little reflection 2 years later.  Reflecting back when you were just a naive little punk!

You really are the Lawn Mower Man.  How dare you surpass me in the top-100!

Punk!

Regards,
Scrapdog (24)