i am writing a 2D game in C# windows form application.
I want to create Bubble Trouble game, but i'm troubling with the jump algorithm.
I found that i have to use sine function to make it realistic.
There is the ball class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace BubbleTrouble
{
class Ball
{
int level,gapX=2,gapY=2,x,y;
bool up;
public static int[] levelsPX={0,1,2,3,6,8,9,11};
Panel panel;
Graphics gr;
public Ball(int lvl,int x,int y,bool isUp,Graphics gr)
{
this.gr = gr;
up = isUp;
this.x = x;
this.y = y;
if (up)
gapY = -gapY;
level = lvl;
}
public Ball[] pop()
{
if (level == 0)
return null;
else
{
bool up1 = true;
if(y>=20)
up1=false;
Ball first = new Ball(level - 1, x, y,up1,gr);
Ball second = new Ball(level - 1, first.x + 10 * levelsPX[level-1], y, up1,gr);
Ball[] a = { first, second };
level = 0;
return a;
}
}
public void paint()
{
gr.DrawImage(BubbleTrouble.Properties.Resources.ball, x, y, 10 * levelsPX[level], 10 * levelsPX[level]);
}
public void getNextPos()
{
if ((y <= 0) || (y + 10 * levelsPX[level] >= 364))
{
gapY = -gapY;
up = !up;
}
if ((x + 10 * levelsPX[level] >= 681) || (x <= 0))
gapX = -gapX;
if (y < 70)
gapY = -gapY;
//Math.sin
x=x+gapX;
y=y+gapY;
}
}
}
The ball has X and Y integers, and it also have 2 integers gapX and gapY.
gapX and gapY is used to change the direction of the ball, for example right now the ball is in position (30,30) so in that case, the next position will be (30+gapX,30+gapY), and after this (30+2gapX,30+2gapY) and when the ball is get out of the form bound so the gap change sign. For example if the position on X scale is more then the form bound, the change the gapX to -gapX so the ball will move in the other direction.
But, i want the ball jump in wave and i don't know how to do this using the X Y of the ball and the gapX and gapY.
I found that sine function will help me but i still don't know where and how to use this function.
Please help me i'm trying to figure it out but i don't find any solution.
Thank you,
Asaf.
A small method as a hint. You have to adjust to the height and width of your playground:
/// <summary>
/// Calculates ball bouncing.
/// </summary>
/// <param name="x">X position (0 .. 360)</param>
/// <returns>Returns y position (0 .. 1)</returns>
private double Position(double x)
{
x *= Math.PI / 180;
return Math.Abs(Math.Cos(x)); // Always positive
}
Related
I am trying to write a 2d tilemap based world generationscript but the Mathf.PerlinNoise command only returns 0.4652731!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class WorldGeneration : MonoBehaviour
{
public Tilemap Tilemap;
public Tile GrassTile;
public int width;
public int height;
public float threshold;
void Start()
{
Tilemap.ClearAllTiles();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Debug.Log(Mathf.PerlinNoise(x, y));
if (Mathf.PerlinNoise(x, y) >= threshold)
{
Tilemap.SetTile(new Vector3Int(x, y, 0), GrassTile);
}
}
}
}
}
After executeing the script there are no errors at all! Why is the Mathf.PerlinNoise command only returning the same number?
Afaik Mathf.PerlinNoise generates a pattern with size 1 * 1. Not sure if it not even repeats.
What happens in your case I suspect is that you pass in whole numbers so it always returns the same/similar value.
Try to scale it down to fractions from 0 to 1 like e.g.
Debug.Log(Mathf.PerlinNoise(x / width, y / height));
See e.g. this post which had exactly the same issue
all of them printed the same number : 0.4652731
function Start ()
{
print(Mathf.PerlinNoise(0, 0));
print(Mathf.PerlinNoise(2, 0));
print(Mathf.PerlinNoise(0, 5));
print(Mathf.PerlinNoise(10, 3));
print(Mathf.PerlinNoise(2, 0));
print(Mathf.PerlinNoise(1, 1));
}
PerlinNoise doesn't work with integers; use floats instead, like this:
void Start()
{
Debug.Log(Mathf.PerlinNoise(1.01f, 3.45f));
}
That is because you are passing in integer values to the Mathf.PerlinNoise function. You should scale your x and y values down. Brackeys has a good tutorial on that, I recommend you to watch it.
https://www.youtube.com/watch?v=bG0uEXV6aHQ
I'm trying to make a world object that can use a camera and draw multiple models at once, each with their own position (and rotation, etc. in the future). Except it comes up with a black screen and I don't know why.
I've tried changing the way the matrices are created, adjusted the positions, and nothing.
World:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Simple3DWorld.Objects;
namespace Simple3DWorld
{
public class World
{
public Camera Camera;
public ModelObject[] Objects;
public World()
{
Camera = new Camera(new Vector3(0, 0, 1), Vector3.Zero);
Objects = new ModelObject[100];
AddObject(new Landscape(new Vector3(0, 0, -1)));
}
public void AddObject(ModelObject obj)
{
bool added = false;
for (int i = 0; i < Objects.Length && !added; i++)
{
if (Objects[i] == null)
{
Objects[i] = obj;
added = true;
}
}
}
public void Update(GameTime gt)
{
for (int i = 0; i < Objects.Length; i++)
{
if (Objects[i] != null)
{
Objects[i].Update(gt);
}
}
}
public void Draw()
{
for (int i = 0; i < Objects.Length; i++)
{
if (Objects[i] != null)
{
Objects[i].Draw(Camera);
}
}
}
}
}
Camera:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Simple3DWorld
{
public class Camera
{
public Vector3 Position;
public Vector3 LookingAt;
public float FieldOfView;
public float AspectRatio;
public Camera(Vector3 pos, Vector3 lookAt)
{
Position = pos;
LookingAt = lookAt;
FieldOfView = MathHelper.PiOver4;
AspectRatio = STDW.Width / STDW.Height;
}
}
}
ModelObject:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Simple3DWorld
{
public class ModelObject
{
public Model Model;
public Vector3 Position;
public ModelObject(Model model, Vector3 pos)
{
Model = model;
Position = pos;
}
public virtual void Update(GameTime gt)
{
}
public virtual void Draw(Camera camera)
{
foreach (ModelMesh mesh in Model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = Matrix.CreateTranslation(Position);
effect.View = Matrix.CreateLookAt(camera.Position, camera.LookingAt, Vector3.UnitZ);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(camera.FieldOfView, camera.AspectRatio, 1f, 100f);
}
mesh.Draw();
}
}
}
}
And yes, I have made sure that all methods are being called correctly. What should be showing on screen is my landscape model.
EDIT: Solved. For some stupid, frustrating reason, MonoGame will not render your models if your camera's X and Y (not the up vector) are both 0. If anyone could somehow explain this, I would be infinitely grateful.
Your Camera's Position and LookAt vectors cannot both be equal on the two axes perpendicular to the Up vector. This results in a NaN(not a number).
This situation can also be characterized as a Gimbol Lock, since the rotations about the X(Roll) and Y(Pitch) axes are aligned, making it impossible to determine the Z(Yaw) from the information provided.
The problem occurs in the creation of the View matrix in the following line:
effect.View = Matrix.CreateLookAt(camera.Position, camera.LookingAt, Vector3.UnitZ);
Substituting the argument values:
effect.View = Matrix.CreateLookAt(new Vector3(0,0,1), new Vector3(0,0,0), new Vector3(0,0,1));
Annotated partial source for CreateLookAt at implementation(see here for pass the through call)
public static void CreateLookAt(ref Vector3 cameraPosition, ref Vector3 cameraTarget, ref Vector3 cameraUpVector, out Matrix result)
{
var vector = Vector3.Normalize(cameraPosition - cameraTarget);
Normalize((0,0,1) - (0,0,0)) -> (x,y,z)/Length
Length = (0 - 0)² + (0 - 0)² + (0 - 1)² -> 1
(0/Length,0/Length,1/Length) -> (0/1,0/1,1/1) ->(0,0,1) *OK
var vector2 = Vector3.Normalize(Vector3.Cross(cameraUpVector, vector));
Working inside out:
Cross((0,0,1),(0,0,1)) -> (y1 * z2 - z1 * y2, z1 * x2 - x1 *z2, x1 * y2 - y1 *x2) ->
(0 * 1 - 1 * 0, 1 * 0 - 0 * 1, 0 * 0 - 0 * 0) -> (0,0,0) *Mathematically OK, Geometrically undefined(does not define a plane perpendicular to the input points)
Normalize(Cross((0,0,1),(0,0,1))) -> Normalize(0,0,0)
Length = (0 - 0)² + (0 - 0)² + (0 - 0)² -> 0
(0/Length,0/Length,0/Length) -> (0/0,0/0,0/0) ->(NaN,NaN,NaN) *UH-OH
The NaN results from the division by zero with the 0 being unsigned as defined by IEEE 754.
Since any operation on a NaN results in NaN. The NaN propagates to the World and Projection matrices, resulting in a black screen.
To put it another way:
If you were standing up looking (Camera.Position=(0,0,6)), ignoring the fact your head has to tilt to see them, at your feet(Camera.LookAt =(0,0,0)), is it possible to determine the compass direction, where the Up vector equals Vector3.UnitZ, that you are facing? No. It is not possible, you could be facing any direction on the plane.
If I ask for the World coordinate one unit in front of your feet(1,0,0), I can tell which direction you are facing.
Fixed. All I had to do was move my camera off of 0,0,0. 0,1,0 1,0,0 and 1,1,0 all seemed to work, which means the camera's other two coordinates (not the one that determines whether it's up or down) can't be at the origin. Not too sure why this is a thing, but at least it's fixed now.
I'm currently working on a random grid generation base for game map and I'm kinda stuck at how do I call an instantiated object based on their in game coordinates (not Vector3).
This is my hex generation script:
public HexCell cellPrefab;
HexCell[] cells;
void CreateCell (int x, int z, int i) {
Vector3 position;
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z);
}
I can call the object fine using index of objects' array but I can't wrap my head around the idea of calling it based on its given coordinates.
This is how I give them coordinates based on each hex position;
public class HexCell : MonoBehaviour {
public HexCoordinates coordinates;
}
public struct HexCoordinates {
[SerializeField]
private int x, z;
public int X {
get { return x; }
}
public int Y {
get { return -X - Z; }
}
public int Z {
get { return z; }
}
public HexCoordinates (int x, int z) {
this.x = x;
this.z = z;
}
public static HexCoordinates FromOffsetCoordinates (int x, int z) {
return new HexCoordinates(x - z / 2, z);
}
}
So how do I call/select desired hex based on HexCell coordinates?
So how do I call/select desired hex based on HexCell coordinates?
This is easy to do since you used int instead of float to represent the coordinate. Just loop over the cells array. In each loop, access the coordinates variable from the HexCell component then compare x, y and z values. If they match, return the current HexCell in the loop. If not, simply return null. You can also do this with linq but avoid it.
Your cell array:
HexCell[] cells;
Get HexCell from coordinate:
HexCell getHexCellFromCoordinate(int x, int y, int z)
{
//Loop thorugh each HexCell
for (int i = 0; i < cells.Length; i++)
{
HexCoordinates hc = cells[i].coordinates;
//Check if coordinate matches then return it
if ((hc.X == x) && (hc.Y == y) && (hc.Z == z))
{
return cells[i];
}
}
//No match. Return null
return null;
}
Since the coordinate is in int instead of float, you can also use Vector3Int(Requires Unity 2017.2 and above) to represent them instead of x, y and z. Note that this is different from Vector3.
HexCell getHexCellFromCoordinate(Vector3Int coord)
{
//Loop thorugh each HexCell
for (int i = 0; i < cells.Length; i++)
{
HexCoordinates hc = cells[i].coordinates;
//Check if coordinate matches then return it
if ((hc.X == coord.x) && (hc.Y == coord.y) && (hc.Z == coord.z))
{
return cells[i];
}
}
//No match. Return null
return null;
}
Alternatively, you could use some built-in functions to make it more readable.
HexCell getHexCellFromCoordinate(int x, int y, int z)
{
return cells.FirstOrDefault(
cell =>
cell.x == x &&
cell.y == y &&
cell.z == z
);
}
HexCell getHexCellFromCoordinate(Vector3Int coord)
{
return cells.FirstOrDefault(cell =>
cell.x == coord.x &&
cell.y == coord.y &&
cell.z == coord.z
);
}
It's worth noting, since I'm not sure where you'll be needing to find the cells, that FirstOrDefault makes heap allocations and therefore invoking it too often on too large of a list (as in tens of thousands of times per frame, and/or against tens of thousands of objects) it could result in undesired garbage collections, which may cause a stutter in your application, and that stutter may be noticeable if combined with other code that uses heap allocations willy-nilly.
So as a general rule, start with something simple. If your application starts to slow down or take up too much memory, go back and optimize starting with the hottest (least performant) code. You can use a profiler to help you find those spots. But please, don't riddle your code with for loops just to save some extra cpu cycles.
I have been trying to calculate the average of the raw data that I have been receiving from my camera. The reason is that at the moment I am just using the raw data and this causes quite abit of jitter. Therefore I am wanting the past 5 locations and then an average to be calculated as this would make the hand tracking experience much more smoother.
I have been trying to store the previous 5 locations but when I attempted this I only get the latest raw data which then get stored and overwrites it for the locations.
The OnNewFrame() method is where I get the raw data.
Help on how I would do this would great.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using SDKAttempt;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Drawing;
//The sdk module tracks geometric nodes for every frame
namespace PercUtils
{
class DepthPipeline : UtilMPipeline
{
double screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
double screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
PXCMGesture.GeoNode nData; //data the we will get from the camera
double xPos = 0;
double yPos = 0;
double zPos = 0;
double handOpeness = 0;
private bool finished;
PXCMGesture.GeoNode[] nodeData = new PXCMGesture.GeoNode[1]; //data from hand
public DepthPipeline()
: base()
{
finished = false;
EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_DEPTH);
EnableGesture(); //This function is called to enable finger tracking and gesture recognition
}
public override void OnGestureSetup(ref PXCMGesture.ProfileInfo pinfo) //the application can override this function to fine-tune any parameters during module initilisation
{
base.OnGestureSetup(ref pinfo);
}
public override void OnGesture(ref PXCMGesture.Gesture gesture) //the application needs to receive pose/gesture, the application can override the ongesture function
{
if (gesture.label == PXCMGesture.Gesture.Label.LABEL_POSE_THUMB_UP)
{
Console.WriteLine("Thumb Up"); //debug
}
base.OnGesture(ref gesture);
}
public override void OnAlert(ref PXCMGesture.Alert alert) //the application can override the OnAlert function to receive alert notifications
{
base.OnAlert(ref alert);
}
public override bool OnNewFrame()
{
double xRatio = screenWidth / 160; //corrects the ratio of the x axis of the camera to work with the width screen - scales the camera cood for the screen being used
double yRatio = screenHeight / 120; //corrects the ratio of the y axis of the camera to work with the height screen - scales the camera cood for the screen being used
PXCMGesture gesture = QueryGesture();
pxcmStatus sts = gesture.QueryNodeData(0, PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_LEFT, out nData); //gets the status of the left hand, out nData because the value isn't set and the function must set it before returning it
if (sts >= pxcmStatus.PXCM_STATUS_NO_ERROR)
{
xPos = (screenWidth - (nData.positionImage.x * xRatio)) + (screenWidth / 2);
yPos = ((nData.positionImage.y * yRatio) - (screenHeight / 2));
zPos = nData.positionWorld.y * 100;
handOpeness = nData.openness;
TitleScreen.setX(xPos); //this corrects the inverted control of the dot
TitleScreen.setY(yPos);
TitleScreen.getHandOpenness(handOpeness);
PlayGame.setX(xPos); //this corrects the inverted control of the dot
PlayGame.setY(yPos);
PlayGame.getHandOpeness(handOpeness);
PlayGame.getZ(zPos); //gets the z coordnidate of the users hand.
PlayGameEasy.setX(xPos); //this corrects the inverted control of the dot
PlayGameEasy.setY(yPos);
PlayGameEasy.getHandOpeness(handOpeness);
PlayGameEasy.getZ(zPos); //gets the z coordnidate of the users hand.
CollisionClass.setX(xPos);
CollisionClass.setY(yPos);
CollisionClass.getZ(zPos);
Button.setX(xPos);
Button.setY(yPos);
Button.getHandOpeness(handOpeness);
ReplayScreen.getX(xPos);
ReplayScreen.getY(yPos);
HighScore.getX(xPos);
HighScore.getY(yPos);
DifficultyScreen.getX(xPos);
DifficultyScreen.getY(yPos);
}
return !finished;
}
public void Finish()
{
finished = true;
}
public bool IsFinished()
{
return finished;
}
public void StartWork()
{
finished = false;
this.LoopFrames();
}
}
}
Could you add an array of 5 double for each xpos, ypos, zpos and an indexAverage
Make all of them private class variables
In your new frame function use:
Xpos_array [indexAverage]= (screenWidth - (nData.positionImage.x * xRatio)) + (screenWidth / 2);
Xpos = Xpos_array.select(x => x / 5.0).sum();
[.....]
indexAverage = (indexAverage+1)%5;
The problem that I am having is wrapping my brain around how I could use a single png called Test Map.png:
This has a black border around the screen and smaller stepping blocks for the player to test the collision. I have the gravity working by using a player class and the main class Game1.cs to draw and update the game. I use this ball:
This is my sprite that I move around the screen.
Here is the player.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Gravity_Test_V2
{
class Player
{
public Texture2D Texture;
public Vector2 Velocity;
public Vector2 Position;
public float ground;
private float Speed;
private Rectangle screenBound;
public bool isJumping; //are we jumping or not
public bool goingUp; //if going up or not
public float initialVelocity; //initial velocity
private float jumpU; //How high the player can jump
private float g; //gravity
public float t; //time
private KeyboardState prevKB;
public Player(Texture2D Texture, Vector2 Position, float Speed, Rectangle screenBound)
{
this.Texture = Texture;
this.Position = Position;
ground = Position.Y;
this.Speed = Speed;
this.screenBound = screenBound;
Velocity = Vector2.Zero;
isJumping = goingUp = true;
jumpU = 2.5f;
g = -9.8f;
t = 0;
}
public void Update(GameTime gameTime)
{
Position.X += (Velocity.X * Speed);
//Set the Y position to be subtracted so that the upward movement would be done by decreasing the Y value
Position.Y -= (Velocity.Y * Speed);
goingUp = (Velocity.Y > 0);
// TODO: Add your update logic here
if (isJumping == true)
{
//motion equation using velocity: v = u + at
Velocity.Y = (float)(initialVelocity + (g * t));
//Increase the timer
t += (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (isJumping == true && Position.Y > screenBound.Height - Texture.Height)
{
Position.Y = ground = screenBound.Height - Texture.Height;
Velocity.Y = 0;
isJumping = false;
t = 0;
}
if (Position.X < 0)
{
//if Texture touches left side of the screen, set the position to zero and the velocity to zero.
Position.X = 0;
Velocity.X = 0;
}
else if (Position.X + Texture.Width > screenBound.Width)
{
//if Texture touches left side of the screen, set the position to zero and the velocity to zero.
Position.X = screenBound.Width - Texture.Width;
Velocity.X = 0;
}
if (Position.Y < 0)
{
//if the Texture touches the top of the screen, reset the timer and set the initial velocity to zero.
Position.Y = 0;
t = 0;
initialVelocity = 0;
}
}
public void Input(KeyboardState keyState)
{
if (keyState.IsKeyDown(Keys.Space) && (isJumping == false || Position.Y == ground))
{
isJumping = true;
initialVelocity = jumpU;
}
if (keyState.IsKeyDown(Keys.Left) && !keyState.IsKeyDown(Keys.Right))
{
if (Velocity.X > -1.0f)
{
Velocity.X -= (1.0f / 10);
}
else
{
Velocity.X = -1.0f;
}
}
else if (!keyState.IsKeyDown(Keys.Left) && keyState.IsKeyDown(Keys.Right))
{
if (Velocity.X < 1.0f)
{
Velocity.X += (1.0f / 10);
}
else
{
Velocity.X = 1.0f;
}
}
else
{
if (Velocity.X > 0.05 || Velocity.X < -0.05)
Velocity.X *= 0.70f;
else
Velocity.X = 0;
}
prevKB = keyState;
}
public void Fall()
{
t = 0;
initialVelocity = 0;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
}
}
}
Is there some simple way to make it so the player can collide with the Test Map.png while its inside the Test Map's texture?
EDIT: 1/21/2014 9 AM 'ish'
Level One:
EDIT: 1/21/2014 10:27
I have used a pixle based system to test to see if the player colides with an object but I try to sepreate the object from the play into classes and it will stop working. I mixed both my movment and collision projects together to try and make it work. I took player.cs (witch I have not changed) and added the pixel based collision into the Game1.cs I need to know how to make the player, witch is being controlled by the player.cs class, be seen by the Game1.cs class and used while being called upon by the player.cs class.
Note* I have also changed it so that the game would be using the falling triangles supplied by the pixel based system. I will add the test image when I am able to make this work.
At the moment The player can move and jump but is not reconsidered as colliding.
EDIT: 1/21/2014 10:34
I use 2 projects:
Collision:
http://xbox.create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel
Movment System:
http://gamepopper.co.uk/academic-projects/2012-2/jumping-platformer-example/
I have mixed them up and used pices of them to try and make my own platform.
Game1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Collision_Test
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState prevKB;
Player player;
SpriteFont font;
Texture2D personTexture;
Texture2D blockTexture;
// The color data for the images; used for per pixel collision
Color[] personTextureData;
Color[] blockTextureData;
Vector2 personPosition;
const int PersonMoveSpeed = 5;
public static int screenWidth = 800;
public static int screenHeight = 500;
// Blocks
List<Vector2> blockPositions = new List<Vector2>();
float BlockSpawnProbability = 0.01f;
const int BlockFallSpeed = 1;
Random random = new Random();
// For when a collision is detected
bool personHit = false;
// The sub-rectangle of the drawable area which should be visible on all TVs
Rectangle safeBounds;
// Percentage of the screen on every side is the safe area
const float SafeAreaPortion = 0.05f;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.graphics.PreferredBackBufferWidth = screenWidth;
this.graphics.PreferredBackBufferHeight = screenHeight;
this.graphics.ApplyChanges();
}
protected override void Initialize()
{
base.Initialize();
// Calculate safe bounds based on current resolution
Viewport viewport = graphics.GraphicsDevice.Viewport;
safeBounds = new Rectangle(
(int)(viewport.Width * SafeAreaPortion),
(int)(viewport.Height * SafeAreaPortion),
(int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
(int)(viewport.Height * (1 - 2 * SafeAreaPortion)));
// Start the player in the center along the bottom of the screen
personPosition.X = (safeBounds.Width - personTexture.Width) / 2;
personPosition.Y = safeBounds.Height - personTexture.Height;
}
/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent()
{
blockTexture = Content.Load<Texture2D>("Block");
personTexture = Content.Load<Texture2D>("Person");
font = Content.Load<SpriteFont>("Font");
player = new Player(personTexture, Vector2.Zero, 6.0f, new Rectangle(0, 0,
this.graphics.PreferredBackBufferWidth,
this.graphics.PreferredBackBufferHeight));
// Extract collision data
blockTextureData =
new Color[blockTexture.Width * blockTexture.Height];
blockTexture.GetData(blockTextureData);
personTextureData =
new Color[personTexture.Width * personTexture.Height];
personTexture.GetData(personTextureData);
// Create a sprite batch to draw those textures
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
}
void HandleInput(KeyboardState keyState)
{
player.Input(keyState);
if (prevKB.IsKeyUp(Keys.F) && keyState.IsKeyDown(Keys.F))
{
this.graphics.ToggleFullScreen();
this.graphics.ApplyChanges();
}
}
protected override void Update(GameTime gameTime)
{
// Get input
KeyboardState keyboard = Keyboard.GetState();
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
HandleInput(Keyboard.GetState());
player.Update(gameTime);
prevKB = Keyboard.GetState();
// Allows the game to exit
if (gamePad.Buttons.Back == ButtonState.Pressed ||
keyboard.IsKeyDown(Keys.Escape))
{
this.Exit();
}
// Spawn new falling blocks
if (random.NextDouble() < BlockSpawnProbability)
{
float x = (float)random.NextDouble() *
(Window.ClientBounds.Width - blockTexture.Width);
blockPositions.Add(new Vector2(x, -blockTexture.Height));
}
// Get the bounding rectangle of the person
Rectangle personRectangle =
new Rectangle((int)personPosition.X, (int)personPosition.Y,
personTexture.Width, personTexture.Height);
// Update each block
personHit = false;
for (int i = 0; i < blockPositions.Count; i++)
{
// Animate this block falling
blockPositions[i] =
new Vector2(blockPositions[i].X,
blockPositions[i].Y + BlockFallSpeed);
// Get the bounding rectangle of this block
Rectangle blockRectangle =
new Rectangle((int)blockPositions[i].X, (int)blockPositions[i].Y,
blockTexture.Width, blockTexture.Height);
// Check collision with person
if (IntersectPixels(personRectangle, personTextureData,
blockRectangle, blockTextureData))
{
personHit = true;
}
// Remove this block if it have fallen off the screen
if (blockPositions[i].Y > Window.ClientBounds.Height)
{
blockPositions.RemoveAt(i);
// When removing a block, the next block will have the same index
// as the current block. Decrement i to prevent skipping a block.
i--;
}
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice device = graphics.GraphicsDevice;
// Change the background to red when the person was hit by a block
if (personHit)
{
device.Clear(Color.Red);
}
else
{
device.Clear(Color.CornflowerBlue);
}
spriteBatch.Begin();
player.Draw(spriteBatch);
// Draw blocks
foreach (Vector2 blockPosition in blockPositions)
spriteBatch.Draw(blockTexture, blockPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
/// <summary>
/// Determines if there is overlap of the non-transparent pixels
/// between two sprites.
/// </summary>
/// <param name="rectangleA">Bounding rectangle of the first sprite</param>
/// <param name="dataA">Pixel data of the first sprite</param>
/// <param name="rectangleB">Bouding rectangle of the second sprite</param>
/// <param name="dataB">Pixel data of the second sprite</param>
/// <returns>True if non-transparent pixels overlap; false otherwise</returns>
static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
Rectangle rectangleB, Color[] dataB)
{
// Find the bounds of the rectangle intersection
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);
// Check every point within the intersection bounds
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
// Get the color of both pixels at this point
Color colorA = dataA[(x - rectangleA.Left) +
(y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) +
(y - rectangleB.Top) * rectangleB.Width];
// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
{
// then an intersection has been found
return true;
}
}
}
// No intersection found
return false;
}
}
}
Provided the background of the texture is transparent, you could use Per-Pixel Collision detection.
Essentially it checks the pixels rather than a rectangular box to determine if a collision has occurred. Given your "player" is a ball, it's probably a good idea to use this anyway.
Considering that your map is only black & White, you could process it before starting the game and obtain coords of every Rectangle of black areas, and then use it to use simple collision detection by just checking intersection between the ball bounding box and rectangles.
For example, in pseudocode:
You have a list:
List<Rectangle> rects = new List<Rectangle>();
void LoadMap()
{
Texture2D map = Content.Load<Texture2D>(#"TexturePath");
//Get a 1D array with image's pixels
Color[] map_pixels = new Color[map.Width * map.Height];
map.GetData(map_pixels);
//Convert it in a 2D array
Color[,] map_pixels_2D = new Color[map.Width, map.Height];
for (int x = 0; x < map.Width; x++)
for (int y = 0; y < map.Height; y++)
map_pixels_2D[x, y] = map_pixels[x + y * map.Width];
//**NOTE THAT**: From here it is just an example, probably not working good,
//I wrote it just to share the idea
//Here goes the code to trace rectangles in the map
Rectangle r = Rectangle.Empty;
bool NWvertex_done = false, NEvertex_done = false, SWvertex_done = false;
for (int x = 0; x < map.Width; x++)
{
if (!SWvertex_done)
{
if (map_pixels_2D[x, y+1] == Color.White); //last bottom vertex
{
r.Height = r.Y + y;
SWvertex_done = true;
rects.Add(r);
NWvertex_done = false;
NEvertex_done = false;
r = Rectangle.Empty;
}
}
for (int y = 0; y < map.Height; y++)
{
if (map_pixels_2D[x, y] != Color.White
{
if (!NWvertex_done)
{
SWvertex_done = false;
r.X = x;
r.Y = y;
NWvertex_done = true;
}
else if(!NEvertex_done)
{
if (map_pixels_2D[x, y+1] == Color.White); //last right vertex
{
r.Width = r.X + x;
NEvertex_done = true;
}
}
}
}
}
}
public override void Update(GameTime gametime)
{
//maybe other things
//
foreach (Rectangle rect in rects)
{
//Better with Distance of ball-center and rect
if (ballRect.Intersect(rect))
{
//there is a collision!
//Do something
}
break;
}
//maybe other things
//
}