TGC Codebase Backup



Simplified 3D: Revolve An Object Around Another (No Math) by nonZero

10th Nov 2012 4:14
Summary

Using no complex maths, revolve planets!



Description

By using MOVE OBJECT and ROTATE OBJECT, we can create revolutions around a point without
the need of complex math. This actually save CPU power too. See code for more.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ////////////////////////////////////////////////////////////////
//  Simplified 3D: Revolve An Object Around Another (No Math) //
////////////////////////////////////////////////////////////////
// * This is free to use, however you see fit.                //
// * I accept no liability for loss or damages related to the //
//   use of this code.                                        //
// * I provide no warranty or guarantee of any sort regarding //
//   the code.                                                //
// * A credit to the author is not required, but it is always //
//   appreciated.                                             //
// * Make derivs and improvements on this code as much as you //
//   like but please don't just copy-paste it and claim it as //
//   your own. How will you face your ancestors then?         //
// * THE FORMATTING MAY BE OFF due to the codebase's limited  //
//   number of columns and enforced word-wrapping. As such,   //
//   it is recomended you copy and paste it to your editor    //
//   to view it properly. Notepad will even be sufficient.    //
//                                                            //
// Cheers, nonZero                                            //
////////////////////////////////////////////////////////////////

``` Scenario: You have a scene with a planet revolving around the sun.
``` Problem: You don't have the math skills to calculate the angles and new positions.
``` Solution: Why make work for yourself? Use this method

// Create some constants to make code easier to read later*
   #CONSTANT   objAX    OBJECT ANGLE X(1)
   #CONSTANT   objAY    OBJECT ANGLE Y(1)
   #CONSTANT   objAZ    OBJECT ANGLE Z(1)

// Setup display
   SET DISPLAY MODE DESKTOP WIDTH(), DESKTOP HEIGHT(), 32
   BACKDROP ON: COLOR BACKDROP 0x000000: AUTOCAM OFF
   SYNC RATE 60

// Make planet and position it for better visibility and alignment
   MAKE OBJECT SPHERE 1, 0.5
   POSITION OBJECT 1, -2, 0, 5

// Main loop:
   objectSpeed# = 0.125                                           // This controls the speed.
   objectRevolve# = objectSpeed# * 40                             // This maintains a preset for the revolution arc when changin speeds
   WHILE KEYSTATE(16) = 0
         MOVE OBJECT 1, objectSpeed#                              // Move object in facing direction.
         ROTATE OBJECT 1, objAX, objAY + objectRevolve#, objAZ    // Rotate object on Y-axis. *Remember these are constants
         TEXT 1, 20, "Press q to quit..."
   ENDWHILE