delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/27/08:18:26

From: aho450s AT nic DOT smsu DOT edu (Tony O'Bryan)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Moving a Rotated Sprite
Date: Mon, 26 May 1997 21:36:02 GMT
Organization: Southwest Missouri State University
Lines: 44
Message-ID: <3389fd75.2586660@ursa.smsu.edu>
References: <3387718E DOT 3AFE AT imag DOT net>
NNTP-Posting-Host: forseti.i176.smsu.edu
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Sat, 24 May 1997 15:54:06 -0700, SpankE <dingfam AT imag DOT net> wrote:

>heya
>  I'm working on a 2D racing game with Allegro, and I want to use the
>rotate_sprite() function to spin the cars around. So far, I've had no

Using rotate_sprite(), blit(), and stretch_sprite() at run-time incurs enormous
speed penalties.  You should convert all your rotating sprites to arrays of
compiled bitmaps for run-time use.  It requires much more memory at run-time,
but the performance boost is well worth it.  All my objects in Mythos (my
Gauntlet look-alike game in progress) used to be rotated and blitted (even the
background tiles) using BITMAPs until I found that the speed was unbearable with
as few as 120 tiles (12 by 10) and only 30 or 40 objects displayed on a
640x480x256 system on a Pentium 120.  I (with much distress at my lack of
forethought) converted them all to compiled bitmap arrays (I'll send you the
source for the conversion if you want it) and was rewarded with an incredible
performance boost.  Now it takes well over 500 simultaneous objects to start
slowing down my Pentium.

>problems with this. However, what I want to know is how do I move the
>rotated cars forwards and backwards relative to their current position.

You need to be familiar with a little trig.  Within each object, keep a record
of the object's current angle (use 0 to 255 for the angle).  Also keep a counter
for fractional units of the object's location.  

Each time the car moves forward, multiply the cosine of the angle by the car's
velocity to get the X increment.  Multiply the sine of the angle by the car's
velocity for the Y increment.  By the way, use the fractional counters for the
multiplication.

Add the whole number of the result to the respective X or Y coordinate.
Subtract the whole number from each of the fractional counters to get the
remainder.  Next time the car moves, add them to the results of the respective
multiplies.  If the object moves backward, add 128 to the forward angle (the
angle should be kept in an 8-bit variable to take advantage of automatic
wrapping).  This will give you the angle directly behind the object.  Perform
the same multiplications as before, etc.

I did this for Mythos (though I could easily clean up some parts of my code
-grin-).  It was much easier to write the code than to try explaining it.  Use
Allegro's fixed-point routines for the trig stuff.

As always: if anyone knows of a better way, I am eager to learn it.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019