Sign in to follow this  

XNA Vector2.Reflect Help

This topic is 2024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[CODE]
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;
texture = content.Load<Texture2D>("ball");
}
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);
}
}
}
[/CODE]

Game1 Class:

[CODE]
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;
Line addingLine;
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()
{
// TODO: Add your initialization logic here
IsMouseVisible = true;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Line.pointTexture = Content.Load<Texture2D>("point");
redPoint = Content.Load<Texture2D>("redpoint");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// 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);
lines.Add(addingLine);
}
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);
}
}
}
[/CODE]

and finally, line class:

[CODE]
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;
points.Add(linePoint);
}
}
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);
}
}

}
[/CODE]

Share this post


Link to post
Share on other sites
A slope of a line doesn't have 2 components.
[CODE]
slope = startPos - endPos;
[/CODE]

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

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

Share this post


Link to post
Share on other sites

This topic is 2024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this