Rotation Algorithm (Lua)

7 views
Skip to first unread message

Adam

unread,
Nov 23, 2008, 1:02:07 AM11/23/08
to xser...@googlegroups.com
(I apologize in advance for the length of this message, but this
method needs to be explained - also, by typing it up, it helps me to
think the idea through more thoroughly)

My demo* now has the ability for the ship to strafe, staying at the
middle of the screen while objects move around it. The panels also
display correctly on their appropriate sides.

The next logical step is to work on rotation. In Ares, the actual
sprite changed every 1/24th of a revolution (where the particular
object had 24 sprites), that being every 15 degrees or .26 radians.
For the in between degrees, the sprite stayed the same and wasn't even
rotated, while the directional arrow pointed to the exact direction.

Now for Xsera, I think we should provide two modes of rotation:
classic rotation, which changes the sprite every interval as the old
game did; and a better rotation system, which would involve rotating
the sprite within its interval, and then switching sprites at the same
intervals as classic rotation, allowing for nicer-looking rotation and
preserving the original look.

My question comes as to the algorithm for either method; I only need
one or the other for my demo, yet both would be nice: the former to
preserve the Ares feel, and the latter for smoother gameplay and
improving upon the old game.

Since it is probably easier to implement, and due to the fact that I
would like to finish this demo by next week, I turn my attention to
the classic rotation method. Measurements will be referenced in
degrees from "Due North".

One form of rotation, which I'm calling "relative sprite rotation",
will be stored in a float type, relative to the amount of rotation
that each sprite has from its own original (there will also be an
absolute rotation value, but that will be used later). This means that
when I switch from one sprite to another, the relative sprite rotation
must be reset from -7.5 (the lowest value on its spectrum) to 7.5, or
vice versa. Since each sprite has a realm of 15 degrees, the sprites
will have a range of the following:
-7.5 to 7.5 (sprite 1)
7.5 to 22.5 (sprite 2)
22.5 to 37.5 (sprite 3)
37.5 to 52.5 (sprite 4)
and so on.

Those are the numbers at which the sprites will change to the next (or
previous) sprite. The trickiest part will be changing the rotation as
the sprites are changed, but that will be dealt with later.

The main question I have with this is the actual Lua script that will
give me my desired results.

Because rotation should begin when a key is pressed down and continue
continuously until the key is released, I'll need to add SDL handling
code that will deal with events if the key is being held down. I
believe that I may have had code for this before, so we'll deal with
this later as well. If not, it should be a 5-minute fix.

The hitch in rotation is the rate of rotation. Each ship, having its
own mass and amount of thrust that the engines can give, has its own
maximum turning rate and turn acceleration. Now acceleration = ∆
velocity (angular in this case) / ∆ time, so for each moment (for an
example's sake, let's say 0.05 seconds) the velocity will have to
change based upon the ship's "turn acceleration" value, multiplied
together (because if you move around the variables in a = ∆v / ∆t,
you get a * ∆ t = ∆v). When the turn rate, or turn velocity,
reaches its maximum, then the acceleration will no longer apply.
Hopefully, a little tinkering with values should get the desired
turning acceleration and rate.

Then comes the next problem: rotation and movement. When I press
"W" (the forward key), I want the ship to move in the direction it is
facing, not move straight up, as my code currently does. Alastair,
you've explained this to me before, but I forgot how to do it. A quick
refresher and I'll have it.

The last and finishing touch for rotation will be the little arrow
that sticks out in front of the ships, showing the true direction that
the ship is facing. This shouldn't be hard, I'll just use the absolute
rotation value, but I couldn't find the pointer sprite. Is there such
a sprite, or must it be drawn from lines? If no sprite, I might need
to add code that will draw OpenGL lines, but I can do that on my own.

Alastair, these questions / problems are mainly directed toward you,
but anyone else who can help would be appreciated. I'll try to get on
at 7:30 AM my time / 3:30 PM your time to chat with you about these,
if you haven't responded to them by then.

I'm excited to see what kind of progress not only this game is making,
but myself as a programmer as well.

All help is appreciated and thanks in advance!

Adam

*I accidently pushed my demo to the master branch. I then pushed it to
my demo branch, realizing my error, but I fear I've messed things up.
The only real changes should be in Demo.lua. Hope it's easily fixable.
Oops!

Alastair Lynn

unread,
Nov 23, 2008, 10:39:26 AM11/23/08
to xser...@googlegroups.com
Hi Adam-

I've already implemented the rotation algorithm. Just pass in the
rotation to draw_sprite.

The format is:
graphics.draw_sprite ( name, location_x, location_y, size_x,
size_y, rotation )

I've stuck with the 1/24th frame because introducing other rotation
will degrade quality and make the lighting look weird as it jumps.

The measurements are in radians.

For the moment, I'd let the turn velocity just be set by having the
keys down.

There's code to draw lines, but I haven't written a Lua API yet.

Alastair

> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google
> Groups "Xsera Development" group.
> To post to this group, send email to xser...@googlegroups.com
> To unsubscribe from this group, send email to xsera-dev+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/xsera-dev?hl=en
> -~----------~----~----~----~------~----~------~--~---
>

Adam

unread,
Nov 23, 2008, 10:48:47 AM11/23/08
to xser...@googlegroups.com
Sweet! I have one little question, but I'll ask you on IRC ;)

Adam
Reply all
Reply to author
Forward
0 new messages