1 / 10

The Perspective of Motion

The Perspective of Motion. Sometimes in 2D games we want to give the impression of depth. Scaling can be used the vary the relative size of sprites to create a 3D effect.

noah
Download Presentation

The Perspective of Motion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Perspective of Motion

  2. Sometimes in 2D games we want to give the impression of depth. Scaling can be used the vary the relative size of sprites to create a 3D effect. If it is desired to give the impression of a constant speed of an object it may be necessary to vary the actual pixels/second speed of a sprite according to its scale factor. Consider the apparent motion of objects as you pass them in a fast moving vehicle. The closer the object, the greater is its change in angular position. 1 2 3 direction of motion

  3. view.Height Y 0.1 scale 2.5 0 0 X view.Width

  4. Scale Used to Set Apparent Size & Angular Motion

  5. Global Parameters AsteroidSpriteDemo GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Rectangle arect; Rectangle ablit; Texture2D asprite; Texture2D bkg; Vector2 spriteorigin; Vector2 spritescale; Vector2 spriteposition; KeyboardState kbstate; Rectangle bkgrect = newRectangle(); int spritecount; int row, col; int frametime = 35; //time in milliseconds int animtime; double vx = 2.5; double vy = -1.4; double x, y; double vscale = 0.998; int maxX, minX, maxY, minY;

  6. Running Full Screen public Game1() { graphics = newGraphicsDeviceManager(this); graphics.IsFullScreen = true; Content.RootDirectory = "Content"; }

  7. AsteroidSpriteDemo Initialize( ) protectedoverridevoid Initialize() { // TODO: Add your initialization logic here Viewport view = graphics.GraphicsDevice.Viewport; maxX = view.Width; x = (double)arect.X; y = (double)arect.Y; arect.Y = 150; arect.Width = 64; arect.Height = 64; ablit.Width = 64; ablit.Height = 64; spriteorigin.X = 0; spriteorigin.Y = 0; spritescale.X = (float)1.0; spritescale.Y = (float)1.0; maxX = view.Width - 3 * arect.Width / 5; minX = -arect.Width / 4; maxY = view.Height - 3 * arect.Height / 4; minY = -arect.Height / 4; arect.X = view.Width / 2; arect.Y = view.Height / 2; spriteposition.X = (float)view.Width / 2; spriteposition.Y = (float)view.Height / 2; row = 0; col = 0; animtime = 0; base.Initialize(); } size of one blit of the asteroid spritesheet the starting point on the asteroid spritesheet default value is (0,0) the relative scale of the asteroid sprite will be limited to a value between 0.1 and 2.5 view limits - takes into account the size of the asteroid sprite location of asteroid in game view

  8. AsteroidSpriteDemo LoadContent( ) protectedoverridevoid LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = newSpriteBatch(GraphicsDevice); asprite = Content.Load<Texture2D>("asteroid"); bkg = Content.Load<Texture2D>("space"); bkgrect.X = 0; bkgrect.Y = 0; bkgrect.Width = bkg.Width; bkgrect.Height = bkg.Height; }

  9. AsteroidSpriteDemo Update( ) protectedoverridevoid Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); kbstate = Keyboard.GetState(); if (kbstate.IsKeyDown(Keys.Q)) this.Exit(); // TODO: Add your update logic here x += vx*spritescale.X; y += vy*spritescale.Y; arect.X = (int)x; arect.Y = (int)y; spriteposition.X = (float)x; spriteposition.Y = (float)y; if (x > maxX || x < minX) vx *= -1; if (y > maxY || y < minY) vy *= -1; spritescale.X *= (float)vscale; spritescale.Y *= (float)vscale; if (spritescale.X<0.1 || spritescale.X>2.5) vscale = 1.0/vscale; animtime += gameTime.ElapsedGameTime.Milliseconds; if (animtime > frametime) { spritecount = (spritecount + 1) % 64; row = spritecount / 8; col = spritecount % 8; ablit.X = col * arect.Width; ablit.Y = row * arect.Height; animtime = 0; } base.Update(gameTime); } press Q to Quit the Demo sprite velocity is factored by spritescale. When scale is small the change in spriteposition per frame is also smaller asteroid bounces off view borders X and Y scale is kept the same (otherwise aspect ratio of sprite would change size of sprite is changed by multiplying by vscale which will be a value either slightly less than 1.0 (0.998) or slightly greater than 1.0 (1/0.998 = 1.002) vscale limits are between 0.1 and 2.5 when elapsed time exceeds animtime a new sprite is selected from the spritesheet one of the 64 asteroid images will be blitted out of the 8 x 8 asteroid spritesheet

  10. AsteroidSpriteDemo Draw( ) protectedoverridevoid Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(bkg, bkgrect, Color.White); spriteBatch.Draw(asprite,spriteposition,ablit,Color.White,(float)0.0,spriteorigin,spritescale,SpriteEffects.None,(float)0.0); spriteBatch.End(); base.Draw(gameTime); } the entire spritesheet where in view the sprite will be drawn the size of the blitting region use White here to maintain original colors the amount of rotation of the sprite the portion of the sprite that is conisdered to be the origin (default is 0,0) the relative scale of the sprite in X and Y can be used to flip the sprite vertically or horizontally determines which sprites are drawn on top if theyoverlap

More Related