public override void Update(GameTime gameTime) { cCollisionMap cm = PrimalDevistation.Instance.Level.CollisionMap; float fpOldX, fpOldY, fpNewX, fpNewY; int oldX, oldY, newX, newY; // Update the particles in a fountain type of way for (int i = 0; i < _maxParticles; i++) { cCPUParticle p = _particles[i]; p._velX += PrimalDevistation.GRAVITY.X; p._velY += PrimalDevistation.GRAVITY.Y; fpOldX = _pointVertices[i].position.X; fpOldY = _pointVertices[i].position.Y; fpNewX = fpOldX + p._velX; fpNewY = fpOldY + p._velY; oldX = (int)fpOldX; oldY = (int)fpOldY; newX = (int)fpNewX; newY = (int)fpNewY; // If we have actually moved a pixel in the last frame (could have been less!) if (oldX != newX) { // If we collide at this new X position we need to rebound if (cm.CheckCollision(newX, oldY)) { p._velX = -p._velX * 0.5f; fpNewX = fpOldX; } } // If we have actually moved a pixel in the last frame (could have been less!) if (oldY != newY) { // If we collide at this new X position we need to rebound if (cm.CheckCollision(oldX, newY)) { p._velY = -p._velY * 0.5f; fpNewY = fpOldY; } } // Finally set the new position _pointVertices[i].position.X = fpNewX; _pointVertices[i].position.Y = fpNewY; } }
protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { ContentManager cm = PrimalDevistation.Instance.CM; GraphicsDevice gd = GraphicsDevice; Random r = new Random(); // Load the standard point sprite render effect _renderEffect = cm.Load <Effect>(@"Shaders/PointSpriteRender"); _texture = cm.Load <Texture2D>(@"Sprites/particle2x2"); // Create the vertex buffer _vertexBuffer = new VertexBuffer(gd, _maxParticles * PointVertex.Size, ResourceUsage.Points | ResourceUsage.WriteOnly, ResourceManagementMode.Automatic); // Create the declaration _vertexDeclaration = new VertexDeclaration(gd, PointVertex.Elements); // init the points _pointVertices = new PointVertex[_maxParticles]; _particles = new cCPUParticle[_maxParticles]; for (int i = 0; i < _maxParticles; i++) { _pointVertices[i] = new PointVertex(); _pointVertices[i].position = new Vector3(-100, -100, 0); _pointVertices[i].color = _colors[r.Next(0, _colors.Length)].ToVector4(); _pointVertices[i].pointSize = 2; _particles[i] = new cCPUParticle(); } // Create a matrix to convert from pixel coordinates to clip space // So, before transformation, the screen boundaries go from 0 to (say) 640 horizontally, // and 0 to (say) 480 vertically. After transformation, they go from -1 to 1 horizontally, // and -1 to 1 vertically. Note in pixel coordinates, increasing Y values are further down // the screen, while in clip space, increasing Y values are further up the screen -- so there // is a Y scaling factor of -1. float viewportWidth = (float)gd.Viewport.Width; float viewportHeight = (float)gd.Viewport.Height; float scaleX = 1.0f / (viewportWidth / 2); float scaleY = 1.0f / (viewportHeight / 2); _proj = Matrix.CreateScale(scaleX, scaleY, 1) * Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(-1, 1, 0); } }