public Vector2D( Vector2D rhs ) { this.x = rhs.x; this.y = rhs.y; }
public GameObject() { pos = new Vector2D( 0.0F, 0.0F ); vel = new Vector2D( 0.0F, 0.0F ); rad = 3; speed = 1; hops = 3; touching = false; color = new byte[3]; coords = new Box2D(); }
public Box2D() { min = new Vector2D(); max = new Vector2D(); }
private void UpdatePhysics() { // Update time passed long timeStamp = clock.ElapsedMilliseconds; long timePassed = timeStamp - lastTick; lastTick = timeStamp; // Check if player will collide float minDist = -1.0F; int width = (int)player.coords.Width(); int height = (int)player.coords.Height(); Vector2D disp = new Vector2D(); Vector2D n = new Vector2D( player.vel.y, -player.vel.x ).Normalized(); Vector2D relPos = new Vector2D( player.pos - player.coords.min ); if ( autoThresh ) objThresh = AutoThresh( ref grays ); for ( int y = 0; y < height; ++y ) { for ( int x = 0; x < width; ++x ) { if ( player.window[y * width + x] < objThresh ) { // Get distance from direction line float distP = Math.Abs( n * new Vector2D( (float)x - relPos.x, (float)y - relPos.y ) ); if ( distP <= player.rad ) { float dist = new Vector2D( relPos.x - x, relPos.y - y ).Mag(); if ( minDist == -1.0F || dist < minDist ) minDist = dist; } } } } // if no collision if ( minDist == -1.0F ) { player.pos += player.vel; if ( player.touching ) { player.vel = new Vector2D(); } } // if collision else { player.touching = true; player.pos += ( minDist * ( 20F / 1000F ) * player.vel ); player.vel = new Vector2D(); } // Update velocity if falling if ( !player.touching ) player.vel.y += ( gravity * (float)timePassed / 1000.0F ); }
private void UpdateGraphics() { // Image from webcam int width = fromCam.Width; int height = fromCam.Height; // Lock bitmap BitmapData data = fromCam.LockBits( new Rectangle( 0, 0, width, height ), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb ); int stride = data.Stride; int step = stride / width; int nBytes = stride * height; IntPtr ptr = data.Scan0; byte[] rgbs = new byte[nBytes]; Marshal.Copy( ptr, rgbs, 0, nBytes ); grays = new byte[width * height]; // Histogram stretch HistStretch( ref rgbs, width, height, step, stride ); // Specify region checked by physics GetRegionBounds( ref player.coords.min, ref player.coords.max ); Vector2D min = new Vector2D( player.coords.min ); Vector2D max = new Vector2D( player.coords.max ); player.window = new byte[(int)player.coords.Width() * (int)player.coords.Height()]; int regionIndex = 0; int radSqrd = player.rad * player.rad; for ( int y = 0; y < height; ++y ) { for ( int xs = 0; xs < stride; xs += step ) { int index = y * stride + xs; int x = xs / step; int gray = ( (int)rgbs[index + 2] + (int)rgbs[index + 1] + (int)rgbs[index] ) / 3; grays[y * width + x] = (byte)gray; // Fill physics update region if ( x >= min.x && x <= max.x && y >= min.y && y <= max.y ) player.window[regionIndex++] = (byte)( gray ); // Draw player int xTrans = x - player.pos.XInt(); int yTrans = y - player.pos.YInt(); if ( xTrans * xTrans + yTrans * yTrans <= radSqrd ) { rgbs[index + 2] = player.color[2]; rgbs[index + 1] = player.color[1]; rgbs[index + 0] = player.color[0]; } } } disp = new Bitmap( width, height ); BitmapData dispData = disp.LockBits( new Rectangle( 0, 0, width, height ), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb ); ptr = dispData.Scan0; Marshal.Copy( rgbs, 0, ptr, nBytes ); // Unlock bitmap fromCam.UnlockBits( data ); disp.UnlockBits( dispData ); }
private void GetRegionBounds( ref Vector2D min, ref Vector2D max ) { if ( player.vel.x >= 0 ) { min.x = player.pos.x - (float)player.rad; max.x = player.pos.x + player.vel.x + (float)player.rad; } else { min.x = player.pos.x + player.vel.x - (float)player.rad; max.x = player.pos.x + (float)player.rad; } if ( player.vel.y >= 0 ) { min.y = player.pos.y - (float)player.rad; max.y = player.pos.y + player.vel.y + (float)player.rad; } else { min.y = player.pos.y + player.vel.y - (float)player.rad; max.y = player.pos.y + (float)player.rad; } min.x = (float)Math.Floor( min.x ); min.y = (float)Math.Floor( min.y ); max.x = (float)Math.Ceiling( max.x ); max.y = (float)Math.Ceiling( max.y ); }