TGC Codebase Backup



Trig calculation, distance angle by HeavyAmp

6th Aug 2010 4:11
Summary

Position from angle and disance



Description



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

void DarkGDK ( void )
{
	dbSyncOn();
	dbSyncRate(60);
	dbDisableEscapeKey();

	//Swivel point of haptic hand pen
	float posX = 100;
	float posY = 100;

	//position of end of pen in relation to the swivel point (offset)
	float YCord;
	float XCord;

	//overall position for the end of the pen
	float EndPosX;
	float EndPosY;

	//The distance is the length between the swivel point and end of pen
	float Distance = 200;

	//Note: In this example all the mouse is doing is giving us an angle to work with

	while(true)
	{
		//------------------------------------------------------------------

		//Get Cords from mouse position
		float MouseX = dbMouseX();
		float MouseY = dbMouseY();

		//Get Distance Between 100,100 and mouse position
		float DistY = MouseY - posY;
		float DistX = MouseX - posX;

		//Get angle between the two points( I think the c++ math equivalent is atan() )
	    float Angle = dbATANFULL( DistY,DistX );

		//In this example we have to calculate the angle but in the haptic hand you
		//should already know the angle of the swivel point so the above code is not needed

		//Work out the offset between the two points 
		//the important bit of code
		YCord = dbSIN( Angle ) * Distance;
		XCord = dbCOS( Angle ) * Distance;

		//Add the offset to the 100,100 position to worked out the overall position
		EndPosX = posX + XCord;
		EndPosY = posY + YCord; 

		//------------------Print out all the coords--------------------
		
		//Print out some white reference lines
		dbInk( dbRGB( 255,255,255), 1);
		dbLine( posX,posY,posX +200,posY );
		dbLine( posX,posY,posX,posY+200  );

		//Print out red Pen
		dbInk( dbRGB( 255,0,0), 1);
		dbLine( posX,posY,EndPosX,EndPosY );
		dbInk( dbRGB( 255,255,255), 1);

		//Print out the X and Y cords of the end of the pen
		char Buffer1[50];
		itoa( EndPosX, Buffer1 ,10);
		dbText( EndPosX,EndPosY, "X:" );
		dbText( EndPosX + 15,EndPosY, Buffer1 );

		char Buffer2[50];
		itoa( EndPosY, Buffer2 ,10);
		dbText( EndPosX + 50,EndPosY, "Y:" );
		dbText( EndPosX + 65,EndPosY, Buffer2 );

		//Print out the Swivel Point
		dbText( 0, 0,"Swivel Point: x:100, y:100" );

		//Print out the angle
		char BufAngle[50];
		itoa( Angle, BufAngle ,10);
		dbText( 0 , 20 , "Angle:" );
		dbText( 50, 20, BufAngle );

		//Print out the length of the pen (Distance)
		dbText( 0 , 40 , "Length of pen: 200" );

		//--------------------------------------------------------------

		if ( dbEscapeKey() )
		{
			break;
		}

		dbSync ( );
		dbCLS();
	}

	return;
}