10 Replies - 470 Views - Last Post: 12 August 2013 - 10:50 AM Rate Topic: -----

#1 ModusOne  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-June 13

Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 07:01 AM

I'm in my college holidays and i though i might as well make a small game, to get a head start on next year and i have come across a problem.

I am basically making a asteroids game and I have made it so my ship can move up and down and it can rotate, but I want to have it , so that my ship will go the way it is facing, so if the ship is rotated 90 degrees to face east, the ship will move in the east direction.

// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple 2D project that uses Dark GDK
// it can be used as a starting point in making your own 2D games

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"

// the main entry point for the application is this function
void DarkGDK ( void )
{
	// in this application a backdrop is loaded and then several
	// animated sprites are displayed on screen

	// when starting a Dark GDK program it is useful to set global
	// application properties, we begin by turning the sync rate on,
	// this means we control when the screen is updated, we also set
	// the maximum rate to 60 which means the maximum frame rate will
	// be set at 60 frames per second
	dbSyncOn   ( );
	dbSyncRate ( 60 );

	// a call is made to this function so we can stop the GDK from
	// responding to the escape key, we can then add in some code in our
	// main loop so we can control what happens when the escape key is pressed
	dbDisableEscapeKey ( );

	// now we will set the random seed value to the timer, this will
	// help us to get more random values each time we run the program
	dbRandomize ( dbTimer ( ) );

	// we are going to display a backdrop for the scene, to do this
	// we load our image and give it an ID number of 1, this particular
	// image is of a sky at night with stars
	dbLoadImage ( "backdrop.bmp", 1 );

	// the next step is to create a sprite that uses this image, this
	// is achieved by calling dbSprite and passing in a value of 1 for the
	// sprites ID, 0 for the X coordinate, 0 for the Y coordinates and a
	// value of 1 for the image
	dbSprite ( 1, 0, 0, 1 );
	

	// next we will load in some animated sprites, before doing this
	// we need to adjust the image color key, by using this function we
	// can make a specific color be transparent, in our case we want this
	// to be bright pink
	dbSetImageColorKey ( 255, 0, 255 );

	// in this loop we're going to create some animated sprites, the image
	// we load contains frames of animation for an asteroid
	
	int g = 2;
	int XCord=100;
	int YCord=100;
	int angle = 0;
	int Cangle = 7; // constant angle
	

	   dbLoadImage("Ship.bmp", g);  
		dbSprite(g, XCord, YCord, g);//this will display the object 
		dbOffsetSprite(2, 50, 50);

		
		
		


	
		
	while ( LoopGDK () )
	{
		 
		  if(dbDownKey())//move down
		  {
			 
YCord++;
YCord++;
YCord++;
YCord++;
YCord++;
			
		  }
		  if(dbUpKey())//move up
		  {		   
			  YCord=YCord-5;		   
		  }

		  if(dbRightKey())//move right
		  {
			dbRotateSprite(2,angle + Cangle);
			angle = angle + Cangle;
		}

		 if(dbLeftKey())
		 {
			 dbRotateSprite(2, angle - Cangle);
			angle = angle - Cangle;
		  }

		 dbSprite(g, XCord, YCord, g);//Set The Cordinates To XCord And YCord
	
	

		  

	
		// here we check if the escape key has been pressed, when it has
		// we will break out of the loop
		if ( dbEscapeKey ( ) )
			break;

		// here we make a call to update the contents of the screen
		dbSync ( );
		  }


	// when the user presses escape the code will break out to this location
	// and we can free up any previously allocated resources
	
	// delete all the sprites
	for ( int i = 1; i < 30; i++ )
		dbDeleteSprite ( i );

	// delete the backdrop image
	dbDeleteImage ( 1 );

	// and now everything is ready to return back to Windows
	return;

	

}




My method of making my ship move up and down wont work i think but i cant think any other way around it.
If I have not worded the question good enough just say.
I am a novice.

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Making Sprite move the way it faces using GDK

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 09:20 AM

I'm a little confused by your code. It looks like you're expecting 2D code to somehow behave 3D, if you see what I mean.
Was This Post Helpful? 0
  • +
  • -

#3 ModusOne  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-June 13

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 10:25 AM

I'm not sure what you mean.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 12:12 PM

Okay, let's start from the beginning. Do you know what mathematical concept of a vector is?
Was This Post Helpful? 0
  • +
  • -

#5 ModusOne  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-June 13

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 12:52 PM

I think so, i believe i have done bits in high school and college
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 02:08 PM

Okay. Go and revise them and see if they give you any clues.
Was This Post Helpful? 0
  • +
  • -

#7 ModusOne  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-June 13

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 02:22 PM

Ok, will do. Any specific areas that will be needed.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 02:30 PM

Sure. Just pay attention to translations and rotations.
Was This Post Helpful? 0
  • +
  • -

#9 ModusOne  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-June 13

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 02:30 PM

Oki Doki.

Thankyou
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Making Sprite move the way it faces using GDK

Posted 11 August 2013 - 03:16 PM

No problem.
Was This Post Helpful? 0
  • +
  • -

#11 ModusOne  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-June 13

Re: Making Sprite move the way it faces using GDK

Posted 12 August 2013 - 10:50 AM

I have done it now, but it is a very basic and simple way of doing it, I basically set a variable of Speed which is how fast it will move then said
dbMoveSprite(2,SVelocity)

then used
dbMoveSprite(2,-SVelocity)

to stop again.

Then I did a what i would call, a rotation check, in which i would check the rotation of the sprite then save the angle as a variable.

Angle -= SRotationA;
	  dbRotateSprite(2,Angle);


I don't know if this is the perfect way of doing this but It works. I wouldn't know how to do it any other way.

I found something of other forums where the problem was that you had to use some Trig to work out the angles because the base co-ordinates of the sprite were in the top left corner but there is a simple function that allows you to re-position the sprite co-ordinates, well i think that's how it works. dbOffsetSprite.

If there is any way i can improve this please let me know, I am pretty novice at this, so i'm looking to do this the most efficient way.

Thanks for help.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1