TGC Codebase Backup



GDK> 2D top down 360 degrees Movement by Swift

19th Aug 2008 7:23
Summary

Moves an object in the given direction with the given speed and then updates the objects x&y positions to reflect this movement



Description

/*

FUNCTION: MoveDirSpeed( float direction, float speed, float *x, float *y );

DESCRIPTION: Moves an object in the direction with speed and then
updates the objects x&y positions to reflect this movement

direction - is the direction you want the object to move in (from 0-360) 0 is east 90 is north
speed - is the speed you want the object to move at
*x - is a pointer to the address of the objects x position
*y - is a pointer to the address of the objects y position



EXAMPLE: MoveDirSpeed( BallDirection, BallSpeed, &Ball.x, &Ball.y);

MADE BY: Swift

*/



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    


#include "DarkGDK.h"	

// The function prototype
void MoveDirSpeed( float direction, float speed, float *x, float *y );	

// The function
void MoveDirSpeed( float direction, float speed, float *x, float *y )
{			
float pi=3.14159265358979;	
// Declare variable pi for calculations
						
/// Check to see which quadrant the direction angle is in
		if ( direction < 90 && direction >= 0 )					{

// Break the vector direction down into its horizontal and vertical components
// and adjust the objects x&y co-ords correspondingly
		*x +=  speed * dbCos(direction);				            
                     *y += -speed  * dbSin(direction);			
                     }

		if ( direction < 180 && direction >= 90 )		
		{
		*x += - speed  * dbCos(180-direction);
		*y += - speed  * dbSin(180-direction);
		}

		if ( direction < 270 && direction >= 180 )
		{
		*x += - speed  * dbCos(direction-180); 
		*y +=  speed  * dbSin(direction-180); 
		}
	
		if ( direction < 360 && direction >= 270 )     
		{
		*x +=  speed  * dbCos(360-direction);
		*y +=  speed  * dbSin(360-direction);
		}
}