Calculating XY positions and setting boundries
Hey
This is my problem:
I want to set several star objects at random and within certain boundry in window using 2 classes. One class is for checking position:
**************************************************
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Starglider
{
public class CObjPos
{
private Vector2 position;
private Vector2 screenSize;
private Vector2 finalPos;
private Random rndstar;
private int xwidth;
private int yheight;
//based on the windows width and height. trying to pass in
public CObjPos(int width, int height)
{
xwidth = width;
yheight = height;
}
//these methods checks other objects position
private void CalcXOffset()
{
finalPos.X = position.X - (screenSize.X / 2); //calculates the final offset of x in the center of screen
}
private void CalcYOffset()
{
finalPos.Y = position.Y - (screenSize.Y / 2); //calculates the final offset of y in the center of screen
}
private void CalcStarXOffset()
{
rndstar = new Random();
//how do i calculate position based on the windows width and at random?
position.X = rndstar.Next(xwidth);
//readablity when i've defined the finalpos as it is based on what value position has.
finalPos.X = position.X;
//supposed to check boundry so that it only set stars within the width of the window. I dont think it works though.
if (finalPos.X >= xwidth)
{
finalPos.X = 0;
}
}
//same here
private void CalcStarYOffset()
{
rndstar = new Random();
//height?
position.Y = rndstar.Next(yheight);
finalPos.Y = position.Y; //same as last one.
if (finalPos.Y >= yheight)
{
finalPos.Y = 0;
}
}
public float X
{
get {return position.X;}
set {position.X = value; CalcXOffset();}
}
public float Y
{
get {return position.Y;}
set {position.Y = value; CalcYOffset();}
}
public float StarXPos
{
set {finalPos.X = value; CalcStarXOffset();}
}
public float StarYPos
{
set {finalPos.Y = value; CalcStarYOffset();}
}
****************************************
The other class is only a sprite class for drawing the things i want to draw and a third class, namely the mainwindow(form) class where the form properties are..
Question 1
How do i set the stars object at random and within a specified boundry?