Skip to content

Instantly share code, notes, and snippets.

@kenpower
kenpower / gist:3782654
Created September 25, 2012 15:40
Drawing regular polygons in OpenGL
const double PI=3.14159265358979323846;
double r=100;
int sides=10;
glBegin(GL_LINE_STRIP);
for(int i=0;i<sides;i++){
double angle=i*2*PI/sides;
glVertex2d(400+r*cos(angle),300+r*sin(angle));
}
glEnd();
@kenpower
kenpower / gist:3782664
Created September 25, 2012 15:42
Drawing a triangle in pixel space in SFML/OpenGL
while (App.isOpen())
{
// Process events
sf::Event Event;
while (App.pollEvent(Event))
{
// Close window : exit
if (Event.type == sf::Event::Closed)
App.close();
@kenpower
kenpower / gist:3852682
Created October 8, 2012 14:00
Simple collision response between two equally massive objects (Assumed to be spherical)
internal static void bounce(Shape shape1, Shape shape2)
{
Vector2 displacement = shape1._position - shape2._position;
Vector2 closingspeed = shape1._velocity - shape2._velocity;
//check if they are already moving apart
if(Vector2.Dot(displacement,closingspeed)>=0){
return;// if moving apart, do nothing
}
@kenpower
kenpower / gist:3908061
Created October 17, 2012 20:45
Draw 2D line in XNA using a streched 1x1 sprite
Texture2D t; //base for the
protected override void LoadContent()
{
// create 1x1 texture for line drawing
t = new Texture2D(GraphicsDevice, 1, 1);
t.SetData<Color>(
new Color[] { Color.White });// fill the texture with white
}
@kenpower
kenpower / gist:3945700
Created October 24, 2012 12:00
Rotate OpengL box about centre
glTranslated(550,250,0);
glRotated(angle,0,0,1);
glTranslated(-550,-250,0);
glBegin(GL_LINE_LOOP);
glVertex2d(500,200);
glVertex2d(600,200);
glVertex2d(600,300);
glVertex2d(500,300);
glEnd();
@kenpower
kenpower / gist:4018118
Created November 5, 2012 16:27
3D OpenGL Cube in SFML
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _DEBUG
#pragma comment(lib,"sfml-graphics-d.lib")
#pragma comment(lib,"sfml-audio-d.lib")
#pragma comment(lib,"sfml-system-d.lib")
#pragma comment(lib,"sfml-window-d.lib")
@kenpower
kenpower / gist:4032551
Last active March 23, 2017 11:16
Using Assimp aiVector3D and aiMatrix3x3
/*+++++++++++++++++++++++++++++++++++++++++
Some sample code showing how to do basic vector operations with assimp
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// create a 3d Vector
aiVector3D a(1.0f,2.0f,3.0f);
aiVector3D b=a; //copy vector to another
b.x=0.f; //assign value to x component of b
//out put a vector
@kenpower
kenpower / gist:4038604
Created November 8, 2012 12:48
Scrolling Background Terrain Drawing
int vpw = graphics.GraphicsDevice.Viewport.Width;
int vph = graphics.GraphicsDevice.Viewport.Height;
Rectangle rect = new Rectangle(0, 0, vpw, vph);
spriteBatch.Begin();
//construct a rectangle to select an area of background to draw
//position(upper left of) rectangle is camera position minus halh size of viewport
Rectangle terrainRect=new Rectangle(
@kenpower
kenpower / gist:4038610
Created November 8, 2012 12:50
drawing enemies with a moving overhead 2d camera
//drawing enemies with a moving camera
Vector3 cameraPos = new Vector3(-camera.Position.X, -camera.Position.Y, 0);
Matrix cameraTrans = Matrix.CreateTranslation(cameraPos);
spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null,cameraTrans);
foreach(Sprite s in sprites)
s.Draw(gameTime,spriteBatch, true);
spriteBatch.End();
@kenpower
kenpower / gist:4038613
Created November 8, 2012 12:50
Drawing infinite scrolling translucent clouds
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, null, null);
spriteBatch.Draw(clouds,
new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width,
graphics.GraphicsDevice.Viewport.Height),
new Rectangle((int)cloudPos.X, (int)cloudPos.Y, 300,300),
Color.White);
spriteBatch.End();