• Create Account

## XNA Vector2.Reflect Help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Kaandimandimanbopbobop  Members

109
Like
0Likes
Like

Posted 20 May 2012 - 02:13 PM

Hi, I am working on a game, and I need help with my ball bouncing off a line(which is a list of Vector2s). Right now, I am trying to use the Vector2.Reflect(Vector2, Vector2) to bounce in the ball's CheckCollision method, but it is not working. When the ball hits the line, it simple dissapears. All help would be greatly appreciated.

Here is the ball class:

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 Geometry
{
class Ball
{
Vector2 Location;
Vector2 Direction;
Vector2 targetLocation;
Texture2D texture;
int Power;
int PowerRemoveTimer = 50;
public Ball(Vector2 position, Vector2 target, ContentManager content, int speed)
{
Location = position;
targetLocation = target;
FindDirection();
Power = speed;
}
public void FindDirection()
{
Direction = targetLocation - Location;
Direction.Normalize();
}
public void Move()
{
Location += Direction * Power;
}
public void CheckCollision(List<Line> lines)
{
foreach (Line l in lines)
{
foreach (Vector2 point in l.points)
{
if (Vector2.Distance(point, Location) < 20)
{

Direction = Vector2.Reflect(Direction, l.GetPerpendicular());
Direction.Normalize();
}
}
}
}

public void Draw(SpriteBatch spriteBatch)
{
if (PowerRemoveTimer > 0)
{
PowerRemoveTimer--;
}
if (PowerRemoveTimer == 0)
{
Power--;
PowerRemoveTimer = 50;
}
CheckCollision(Game1.lines);
Move();
spriteBatch.Draw(texture, Location, Color.Yellow);
}
}
}


Game1 Class:

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 Geometry
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D redPoint;
List<Vector2> clickedPoints = new List<Vector2>();
public static List<Line> lines = new List<Line>();
MouseState newMouseState;
MouseState oldMouseState;
Ball ball;
KeyboardState newKeyBoardState;
KeyboardState oldKeyBoardState;
Vector2 PointOne;
Vector2 PointTwo;
Line directionLine;
int Power = 10;
Vector2 startPoint = new Vector2(400, 400);

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content.  Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
IsMouseVisible = true;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// </summary>
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
directionLine = new Line(new Vector2(newMouseState.X, newMouseState.Y), startPoint, Color.Green);
newMouseState = Mouse.GetState();
newKeyBoardState = Keyboard.GetState();
if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
{
PointOne = new Vector2(newMouseState.X, newMouseState.Y);
}
if (newMouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released)
{
PointTwo = new Vector2(newMouseState.X, newMouseState.Y);
}
if(newKeyBoardState.IsKeyDown(Keys.Enter) && oldKeyBoardState.IsKeyUp(Keys.Enter))
{
addingLine = new Line(PointOne, PointTwo, Color.Red);
}
if (newKeyBoardState.IsKeyDown(Keys.Escape) && oldKeyBoardState.IsKeyUp(Keys.Escape))
{
lines.Clear();
}
if (newKeyBoardState.IsKeyDown(Keys.OemMinus) && oldKeyBoardState.IsKeyUp(Keys.OemMinus))
{
if (Power > 1)
{
Power--;
}
}
if (newKeyBoardState.IsKeyDown(Keys.OemPlus) && oldKeyBoardState.IsKeyUp(Keys.OemPlus))
{
if (Power < 11)
{
Power++;
}
}
if(newKeyBoardState.IsKeyDown(Keys.Space) && oldKeyBoardState.IsKeyUp(Keys.Space))
{
ball = new Ball(startPoint, new Vector2(newMouseState.X, newMouseState.Y), Content, Power);
}

oldMouseState = Mouse.GetState();
oldKeyBoardState = Keyboard.GetState();

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.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
if (ball != null)
{
ball.Draw(spriteBatch);
}
if (PointOne != null)
{
spriteBatch.Draw(redPoint, PointOne, Color.White);
}
directionLine.Draw(spriteBatch);
spriteBatch.Draw(redPoint, new Rectangle(10, 10, Power * 20, 15), Color.Green);
foreach (Line l in lines)
{
l.Draw(spriteBatch);
}
if (PointTwo != null)
{
spriteBatch.Draw(redPoint, PointTwo, Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}


and finally, line class:

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 Geometry
{
public class Line
{
public Vector2 startPos;
public Vector2 endPos;
public List<Vector2> points = new List<Vector2>();
public static Texture2D pointTexture;
Color color;
public Vector2 slope;

public Line(Vector2 start, Vector2 end, Color col)
{
slope = startPos - endPos;
slope.Normalize();

startPos = start;
endPos = end;
color = col;
findPoints();
}
public void findPoints()
{
Vector2 linePoint = startPos;
float xIncrement = (endPos.X - startPos.X) / 10000;
float yIncrement = (endPos.Y - startPos.Y) / 10000;
for (int i = 0; i < 10000; i++)
{
linePoint.X += xIncrement;
linePoint.Y += yIncrement;
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Vector2 point in points)
{
spriteBatch.Draw(pointTexture, point , color);
}
}
public Vector2 GetPerpendicular()
{
return new Vector2(slope.Y, -slope.X);
}
}

}


### #2Michael Tanczos  Staff Emeritus

5675
Like
0Likes
Like

Posted 24 May 2012 - 08:26 AM

If you put a breakpoint on this line:
Direction = Vector2.Reflect(Direction, l.GetPerpendicular());

Does l.GetPerpendicular match up with what you think it should be? Ie. is it a vector pointing in the right direction

### #3dAND3h  Members

220
Like
0Likes
Like

Posted 26 May 2012 - 12:49 PM

A slope of a line doesn't have 2 components.
slope = startPos - endPos;


It should be just a float value
Slope = Y2 - Y1 / X2 - X1

Your getPerpindicular() function should create a new vector2 based on the negative reciprocal of the slope of your line.

Edited by dAND3h, 26 May 2012 - 12:50 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.