[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] exact algebraic numbers?
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] exact algebraic numbers?
- From: René Dudfield <renesd@xxxxxxxxx>
- Date: Sun, 29 Nov 2009 12:33:58 +0100
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sun, 29 Nov 2009 06:34:00 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=Yr9MHBUh4VX0s+CxfnV/tzrdoTV/MgQ00aVZlM4IGw8=; b=f84QyRBsxB2eEQRO9TLtfECkrjjvov1/+ZYQXyOHi0vUOuRruDTa3MPytwqesVMc86 NAf5jP3+EPRa/kZ/3kvnEADsBC6ZvJZUqV4JLj1C8W+AQVn9Up/IH88/XSNuohH5AVcN gXyivJ/gdMwYhdQH6m/JjB6gRveYDDn6BLZ9Y=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=XeQ69WKjqcX9fS8EXQN7ahSen4djLmsd5yz0lmWakkzjXfcG092WbJj7G6CGMSeD8Z xDa8RDg91tidKHoYRA23uj8jEtkFwIyQpKAf5kj0kWnzs3jrAKRqZdSTvsPBaoYmTV1i ZB2pYPlNXRNYptHdTFHa+g9xPB6yLwTefcMFM=
- In-reply-to: <4B11FFC5.5080107@xxxxxxxxxxxxxx>
- References: <4B11FFC5.5080107@xxxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
On Sun, Nov 29, 2009 at 5:59 AM, Michael George <mdgeorge@xxxxxxxxxxxxxx> wrote:
>
> Hello,
>
> For my game I need to represent right isosceles triangles, and do operations including translation and rotation in 15 degree increments. Unfortunately, I cannot tolerate rounding error, because it will cause bad visual and gameplay artifacts. I'm thinking of representing points as vectors where the components are of the form
>
> a + b * sqrt(2) + c * sqrt(3) + d * sqrt(6)
>
> where a, b, c, and d are rationals. I believe numbers of this form are sufficient for my purposes, although I haven't worked out all the proofs yet. Anyway, I was wondering if anyone has done anything similar and can suggest a library I can use for manipulating such vectors before I go off and implement it myself.
>
> Thanks!
>
> --Mike
hi,
python 2.6 and 3.1 have a fractions module:
http://docs.python.org/library/fractions.html
The Fraction class inherits from the abstract base class numbers.Rational:
http://docs.python.org/library/numbers.html#numbers.Rational
To avoid rounding (and other numerical) errors, the typical technique
is to apply the transformations each time from the identity matrix.
So in a game where a rotation happens at say 1.0 - 1.444 degrees each
frame, you keep count of the total rotation (eg 1.444) and apply the
total rotation every frame. Rather than applying the 1.0 rotation one
frame, and then applying the 1.444 rotation the next frame. Over
10,000 frames or so, you can see there would be little rounding error.
You can also use integers instead of floats, but divide the value by
1,000,000 or so. eg, 1.444 degrees could be 1,444,000. Then as you
do not get the floating point problems. If all of your angles are
between 0-360, and the angle increments are also small it turns out
ok. Python has a long type, so you can get a lot of precision this
way, and avoid floating point problems somewhat. Just before you use
the rotation, you can divide the number by 1,000,000 or so(1444000 /
1000000 == 1.444).
cu.