19
XNA Basic Displaying Image & Collision Detect

XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Embed Size (px)

Citation preview

Page 1: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

XNA Basic

Displaying Image

&

Collision Detect

Page 2: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

What’s format image that XNA support?

XNA support only .bmp .png and .jpg image. .PNG have transparent region for draw on

another image. Usually, we’ll use .jpg for background

and .png for object on background.

Page 3: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

How to include image in project?

Fist right click at content. Second choose add Exiting Item. Third choose your image.

Page 4: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Sample Code how to load into content manager.

Texture2D exceed;

protected override void LoadContent()

{

exceed = Content.Load<Texture2D>(“exceed”);

}

Type File or folder name

Page 5: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

How to assign position?

There are two way to assign position.

- Vector2

- Rectangle

Page 6: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Rectangle

There are two type of Rectangle- DestinationRectangle- SourceRectangle

Both type of Rectangle is a parameter in SpriteBatch.Draw() but isn’t a class.

We’ll talk detail about DestinationRectangle but you can find more information about SourceRectangle in wiki

Page 7: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

DestinationRectangle

Rectangle DestinationRectangle = new Rectangle(0,0,300,400);

The first two parameter is a x,y position. The second two parameter is size of picture.

First of the second two parameter is a width. Second of the second two parameter is a height.

Parameter type is int.

Page 8: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Vector2

Vector2 position = new Vector position(0,0); Parameter is a x,y position and parameter

type is float.

Page 9: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Which one we should use for assign position?

It’s up your design. Vector2 can give position in float that you

can assign pass thumbnail stick. Rectangle can give position in int but can

assign size of your picture.

Page 10: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Axis in XNA

There are two axis in XNA.– One is a axis of display screen.– Two is a axis of picture.

Axis of display screen is for assigning position of picture origin on screen.

Axis of picture is for assign origin.

Page 11: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Axis

Suppose we assign origin on (50,100) screen.

And assign origin on (-50,-50) picture

50

100

X+

Y+

-50

-50X+

Y+

Page 12: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Assume move picture from x = 50 to x=80.

100

X+

Y+

50

-50

-50X+

Y+

-50

-50X+

Y+

80

Page 13: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Origin

Origin is a position that show picture as previous section and we can assign origin from axis of picture.

Origin also is used for rotating picture. You can find more information about rotating

in wiki.

Page 14: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Collision Detecting

There are many collision algorithm. I will show two sample.

Page 15: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Vector2 Collision

bool CollisionDetect(Vector2 Player,Vector2 enemy)

{

float distance = Vector2.DistanceSquared(player,enemy);

if(distance*distance < 10f)

{ return true;}

return false;

}

Page 16: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Rectangle Collision

Bool RectangleCollision(Rectangle Player,Rectangle Enemy)

{return !( Player.Bottom > Enemy.Top || Player.Top < Enemy.Bottom || Player.Right < Enemy.Left|| Player.Left > Enemy.Right)

//check case that is not collision and revert it}

Page 17: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Let’s practice more

Write a program that picture can move around but picture is not out side screen display.

Page 18: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

What’s code we should know?

GraphicsDeviceManager graphics; int displayheight = graphics.GraphicsDevice.Viewport.Height; It’s for height of display screen.

spriteBatch.Draw(Texture2D,Rectangle,Color)

spriteBatch.Draw(Texture2D,Vector2,Color)

Page 19: XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region

Detecting base-level it’s enough for moving picture around.