Difference between revisions of "Game Programming Class3"

esse quam videri
Jump to: navigation, search
(Unity Basics and Movement)
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Class 3 Input Handling=
 
=Class 3 Input Handling=
  
 +
==Overview==
  
 +
Objectives
 +
*Time corrected move on delta time Unity
 +
*SpriteBatch option in monogame
 +
*Review Input class in monogame
 +
*Movement in Unity
 +
Skills
 +
*SVN Commit
 +
*Unity Package
 +
Demo
 +
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/SimpleMovementWRotate Monogame\3.5\SimpleMovementWRotate] :The SimpleMovementWRotate uses the incorrect origin for rotation on purpose. Uncomment second Draw call to fix
 +
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/SpriteBatchOptions Monogame\3.5\SpriteBatchOptions] : many many ways to call Spritebatch draw
 +
*[https://github.com/dsp56001/GameProgramming/tree/master/Unity/SimpleUpdateMovement/Packages Unity\SimpleUpdateMovement\Packages\SimpleMovement.unitypackage] : Unity basic movement
  
 +
==SpriteBatch Options==
  
==in class==
+
'''[http://msdn.microsoft.com/en-us/library/bb203919.aspx 2D Graphics Overview]'''
  
Extract sprite from pacman class and build new project
+
About textures and Batching
  
http://www.xnawiki.com/index.php?title=Basic_Sprite_Class
+
[http://msdn.microsoft.com/en-us/library/bb203889.aspx Displays, ViewPorts, Client Bounds]
  
===Sprite class===
 
  
IntroPacManComponent.zip
+
===Alpha channels===
  
Project that refactors the pacman class into two classes
+
Demo
 +
* [http://gimp.org Gimp]
 +
* [http://www.getpaint.net/ Paint.NET]
  
Sprite.cs
+
===SpriteBatch===
DrawableSprite.cs
 
  
Then the pacman class can inherit from one of these two classes.
+
demo project
 +
*IntroSimpleSpriteBatchOptionsWindows.zip
  
PacMan.cs
+
spriteBatch.Begin();
  
Due to the desire to limit the number of spritebatches, I've created two classes. The sprite class cannot draw itself without being associated with and external spritebatch.
+
[[Image:IntroSpriteModesDemo.png]]
The DrawableSprite has it's own spritebatch which makes it easier to use but less efficient.
 
  
===Classes and Generics===
+
# Options SpriteBlendMode
Shooting with pacman
+
# Additive Enable additive blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-3.aspx
 +
# AlphaBlend Enable alpha blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-2.aspx
 +
# None No blending specified.
  
List<Type> Safer than Arraylist and easier to manage than Array
+
 
 +
 
 +
 
 +
 
 +
==Inclass Keyboard input demo==
 +
 
 +
<code>
 +
KeyboardState keyboardState = Keyboard.GetState();
 +
 
 +
Explore Limitations of Built in Keyboard state.
 +
 
 +
</code>
 +
 
 +
==Input==
 +
input from Game Controller and Keyboard
 +
 
 +
'''GamePad'''
 +
[[Image:XBoxGamePadWithLabels.png]]
 +
 
 +
XNA has built in support for up to 4 game controllers. The default controller is a x-box controller.
 +
X-Box Controller The Controller is accessed through the [http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.gamepad.aspx GamePad] Object and the [http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.gamepadstate_members.aspx GamePadState] Structure reference types
 +
<syntaxhighlight lang="csharp">
 +
        //Get an instance of the gamePadState Structure
 +
        GamePadState gamePad1State = GamePad.GetState(PlayerIndex.One);
 +
       
 +
/*Since the Keyboard structure is Windows only we
 +
need to use some preprocessor
 +
directives to only compile the KeyBoard
 +
state code if the target is not XBOX360 (I
 +
should also add zune if this is one of our build targets)
 +
*/
 +
        #if !XBOX360
 +
            #region KeyBoard
 +
            KeyboardState keyboardState = Keyboard.GetState();
 +
        #endif
 +
       
 +
</syntaxhighlight>
 +
 
 +
Angle Measured in Radians
 +
 
 +
using [http://msdn.microsoft.com/en-us/library/system.math.atan2.aspx atan2] to get the angle from the direction vector
 +
 
 +
 
 +
IntroSimpleSpriteUpdateRotateWindows Project
 +
 
 +
SpriteJump Project
 +
 
 +
==Input Error missing DirectX9==
 +
 
 +
Error:An unhandled exception of type 'System.DllNotFoundException' occurred in SharpDX.XInput.dll Additional information: Unable to load DLL 'xinput1_3.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
 +
 
 +
  XInput library may be missing if you have a fresh install of win8.1 or 10
 +
 
 +
Solution: Install DirectX 9 Runtime http://www.microsoft.com/en-us/download/confirmation.aspx?id=8109 or http://www.microsoft.com/en-us/download/details.aspx?id=35
 +
 
 +
==Unity Basics and Movement==
 +
Demo add Art and setup folders
 +
 
 +
===Unity Input===
 +
Current Unity Input System
 +
 
 +
https://docs.unity3d.com/Manual/ConventionalGameInput.html
 +
 
 +
Proposal to change Unity input system
 +
 
 +
https://blogs.unity3d.com/2016/04/12/developing-the-new-input-system-together-with-you/
 +
https://sites.google.com/a/unity3d.com/unity-input-advisory-board/
 +
 
 +
 
 +
===Basic Movement===
 +
Movement script
 +
 
 +
parameter
 +
<syntaxhighlight lang="csharp">
 +
public Vector3 Direction = new Vector3(1, 0, 0);
 +
    public float Speed = 5;
 +
</syntaxhighlight>
 +
Update
 +
<syntaxhighlight lang="csharp">
 +
void Update () {
 +
 
 +
        //Move Pacman
 +
        this.transform.position += this.Direction * this.Speed * Time.deltaTime;
 +
}
 +
</syntaxhighlight>
 +
 
 +
Full Script with wall bouncing
 +
<syntaxhighlight lang="csharp">
 +
using UnityEngine;
 +
using System.Collections;
 +
 
 +
public class SimpleMove : MonoBehaviour {
 +
 
 +
 
 +
    public Vector3 Direction = new Vector3(1, 0, 0);
 +
    public float Speed = 5;
 +
 
 +
    static Vector3 cameraBottomLeft; //calculate camera left
 +
    static Vector3 cameraTopRight; //calculate camera top
 +
    static Rect cameraRect; //rectangle for camera
 +
 
 +
 
 +
    // Use this for initialization
 +
void Start () {
 +
 
 +
        cameraBottomLeft = Camera.main.ScreenToWorldPoint(Vector3.zero);
 +
        cameraTopRight = Camera.main.ScreenToWorldPoint(new Vector3(
 +
            Camera.main.pixelWidth, Camera.main.pixelHeight));
 +
 
 +
        cameraRect = new Rect(
 +
            cameraBottomLeft.x,
 +
            cameraBottomLeft.y,
 +
            cameraTopRight.x - cameraBottomLeft.x,
 +
            cameraTopRight.y - cameraBottomLeft.y);
 +
 
 +
}
 +
 +
// Update is called once per frame
 +
void Update () {
 +
 
 +
        //Move Pacman
 +
        this.transform.position += this.Direction * this.Speed * Time.deltaTime;
 +
 
 +
        if (!cameraRect.Contains(this.transform.position))
 +
        {
 +
            //keep  on screen
 +
            if (this.transform.position.x <= cameraRect.xMin)
 +
            {
 +
               
 +
                this.Direction.x *= -1;
 +
            }
 +
            if (this.transform.position.x >= cameraRect.xMax)
 +
            {
 +
               
 +
                this.Direction.x *= -1;
 +
            }
 +
            if (this.transform.position.y > cameraRect.yMax)
 +
            {
 +
               
 +
                this.Direction.y *= -1;
 +
            }
 +
            if (this.transform.position.y < cameraRect.yMin)
 +
            {
 +
               
 +
                this.Direction.y *= -1;
 +
            }
 +
           
 +
        }
 +
 
 +
}
 +
}
 +
</syntaxhighlight>
 +
 
 +
and Input from keyboard
 +
<syntaxhighlight lang="csharp">
 +
using UnityEngine;
 +
using System.Collections;
 +
 
 +
public class SimpleInput : MonoBehaviour {
 +
 
 +
 
 +
    public Vector3 Direction;
 +
    public float Speed;
 +
    public Vector3 keyDirection;
 +
   
 +
    // Use this for initialization
 +
void Start () {
 +
 +
}
 +
 +
// Update is called once per frame
 +
void Update () {
 +
        //input
 +
        //direction.x = direction.y = 0;
 +
        keyDirection.x = keyDirection.y = 0;
 +
 
 +
        //Keyboard
 +
        if (Input.GetKey("right"))
 +
        {
 +
            keyDirection.x += 1;
 +
        }
 +
        if (Input.GetKey("left"))
 +
        {
 +
            keyDirection.x += -1;
 +
        }
 +
 
 +
        if (Input.GetKey("up"))
 +
        {
 +
            keyDirection.y += 1;
 +
        }
 +
        if (Input.GetKey("down"))
 +
        {
 +
            keyDirection.y += -1;
 +
        }
 +
 
 +
        Direction += keyDirection;
 +
        Direction.Normalize();
 +
 
 +
 
 +
        //Move Pacman
 +
        this.transform.position += this.Direction * this.Speed * Time.deltaTime;
 +
 
 +
}
 +
}
 +
</syntaxhighlight>
  
 
==Homework==
 
==Homework==
Moving Game
 
* Create an XNA project that uses the Keyboard for input.
 
* Has at least 3 different moving sprite with a common code base.
 
* the sprites should inherit from the same class. Game components are a plus. don't worry about collision yet
 
  
*Read Chapter 3 in XNA 3.0
+
Moving Game 2 Interesting movement (Mono Game and Unity)
 +
 
 +
Update your moving assigment to make the movement more interesting. Use more than 1 input method ie Keyboard, xbox controller, mouse etc.
 +
 
 +
Create a Unity Scene with the same sprite and use a script to create similar movement.
 +
 
 +
Commit the mongame project and an export of the Unity Scene as a package to SVN or Moodle.
 +
 
 +
Repo URL https://iam.colum.edu:8443/svn/GPMonogame3.5/trunk/FA16

Latest revision as of 03:39, 19 September 2016

Class 3 Input Handling

Overview

Objectives

  • Time corrected move on delta time Unity
  • SpriteBatch option in monogame
  • Review Input class in monogame
  • Movement in Unity

Skills

  • SVN Commit
  • Unity Package

Demo

SpriteBatch Options

2D Graphics Overview

About textures and Batching

Displays, ViewPorts, Client Bounds


Alpha channels

Demo

SpriteBatch

demo project

  • IntroSimpleSpriteBatchOptionsWindows.zip

spriteBatch.Begin();

IntroSpriteModesDemo.png

  1. Options SpriteBlendMode
  2. Additive Enable additive blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-3.aspx
  3. AlphaBlend Enable alpha blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-2.aspx
  4. None No blending specified.



Inclass Keyboard input demo

KeyboardState keyboardState = Keyboard.GetState();

Explore Limitations of Built in Keyboard state.

Input

input from Game Controller and Keyboard

GamePad XBoxGamePadWithLabels.png

XNA has built in support for up to 4 game controllers. The default controller is a x-box controller. X-Box Controller The Controller is accessed through the GamePad Object and the GamePadState Structure reference types

         //Get an instance of the gamePadState Structure
         GamePadState gamePad1State = GamePad.GetState(PlayerIndex.One); 
         
/*Since the Keyboard structure is Windows only we 
need to use some preprocessor
directives to only compile the KeyBoard 
state code if the target is not XBOX360 (I
should also add zune if this is one of our build targets)
*/
         #if !XBOX360
            #region KeyBoard
            KeyboardState keyboardState = Keyboard.GetState();
         #endif

Angle Measured in Radians

using atan2 to get the angle from the direction vector


IntroSimpleSpriteUpdateRotateWindows Project

SpriteJump Project

Input Error missing DirectX9

Error:An unhandled exception of type 'System.DllNotFoundException' occurred in SharpDX.XInput.dll Additional information: Unable to load DLL 'xinput1_3.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

  XInput library may be missing if you have a fresh install of win8.1 or 10

Solution: Install DirectX 9 Runtime http://www.microsoft.com/en-us/download/confirmation.aspx?id=8109 or http://www.microsoft.com/en-us/download/details.aspx?id=35

Unity Basics and Movement

Demo add Art and setup folders

Unity Input

Current Unity Input System

https://docs.unity3d.com/Manual/ConventionalGameInput.html

Proposal to change Unity input system

https://blogs.unity3d.com/2016/04/12/developing-the-new-input-system-together-with-you/ https://sites.google.com/a/unity3d.com/unity-input-advisory-board/


Basic Movement

Movement script

parameter

public Vector3 Direction = new Vector3(1, 0, 0);
    public float Speed = 5;

Update

void Update () {

        //Move Pacman
        this.transform.position += this.Direction * this.Speed * Time.deltaTime;
}

Full Script with wall bouncing

using UnityEngine;
using System.Collections;

public class SimpleMove : MonoBehaviour {


    public Vector3 Direction = new Vector3(1, 0, 0);
    public float Speed = 5;

    static Vector3 cameraBottomLeft; //calculate camera left
    static Vector3 cameraTopRight; //calculate camera top
    static Rect cameraRect; //rectangle for camera


    // Use this for initialization
	void Start () {

        cameraBottomLeft = Camera.main.ScreenToWorldPoint(Vector3.zero);
        cameraTopRight = Camera.main.ScreenToWorldPoint(new Vector3(
            Camera.main.pixelWidth, Camera.main.pixelHeight));

        cameraRect = new Rect(
            cameraBottomLeft.x,
            cameraBottomLeft.y,
            cameraTopRight.x - cameraBottomLeft.x,
            cameraTopRight.y - cameraBottomLeft.y);

	}
	
	// Update is called once per frame
	void Update () {

        //Move Pacman
        this.transform.position += this.Direction * this.Speed * Time.deltaTime;

        if (!cameraRect.Contains(this.transform.position))
        {
            //keep  on screen
            if (this.transform.position.x <= cameraRect.xMin)
            {
                
                this.Direction.x *= -1;
            }
            if (this.transform.position.x >= cameraRect.xMax)
            {
                
                this.Direction.x *= -1;
            }
            if (this.transform.position.y > cameraRect.yMax)
            {
                
                this.Direction.y *= -1;
            }
            if (this.transform.position.y < cameraRect.yMin)
            {
                
                this.Direction.y *= -1;
            }
            
        }

	}
}

and Input from keyboard

using UnityEngine;
using System.Collections;

public class SimpleInput : MonoBehaviour {


    public Vector3 Direction;
    public float Speed;
    public Vector3 keyDirection;
    
    // Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        //input
        //direction.x = direction.y = 0;
        keyDirection.x = keyDirection.y = 0;

        //Keyboard
        if (Input.GetKey("right"))
        {
            keyDirection.x += 1;
        }
        if (Input.GetKey("left"))
        {
            keyDirection.x += -1;
        }

        if (Input.GetKey("up"))
        {
            keyDirection.y += 1;
        }
        if (Input.GetKey("down"))
        {
            keyDirection.y += -1;
        }

        Direction += keyDirection;
        Direction.Normalize();


        //Move Pacman
        this.transform.position += this.Direction * this.Speed * Time.deltaTime;

	}
}

Homework

Moving Game 2 Interesting movement (Mono Game and Unity)

Update your moving assigment to make the movement more interesting. Use more than 1 input method ie Keyboard, xbox controller, mouse etc.

Create a Unity Scene with the same sprite and use a script to create similar movement.

Commit the mongame project and an export of the Unity Scene as a package to SVN or Moodle.

Repo URL https://iam.colum.edu:8443/svn/GPMonogame3.5/trunk/FA16