public static PhysRectangle ConstructRectFromTwoPoints(Point a, Point b, float thickness) { float x1 = ConvertUnits.ToSimUnits(a.X); float x2 = ConvertUnits.ToSimUnits(b.X); float y1 = ConvertUnits.ToSimUnits(a.Y); float y2 = ConvertUnits.ToSimUnits(b.Y); thickness = ConvertUnits.ToSimUnits(thickness); float dist = (float)Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2)); float vecX = x1 - x2; float vecY = y1 - y2; float norm1X = (vecX / dist); float norm1Y = (vecY / dist); float rectX = x1 - (norm1X) * dist / 2; float rectY = y1 - (norm1Y) * dist / 2; PhysRectangle rect = new PhysRectangle(GameCanvas.m_PhysicsWorld, new Point(0, 0), ConvertUnits.ToDisplayUnits(dist), ConvertUnits.ToDisplayUnits(thickness)); rect.m_Body.SetTransform(new Vector2(rectX, rectY), (float)Math.Atan2(norm1Y, norm1X)); return rect; }
//Use this override to load any things we need. //This is called right before the main loop is initialized, //after creating the OpenGL context. protected override void OnLoad(EventArgs e) { GL_HEIGHT = Size.Height; GL_WIDTH = Size.Width; GL.LoadIdentity(); GL.MatrixMode(MatrixMode.Projection); GL.Viewport(0, 0, GL_WIDTH, GL_HEIGHT); GL.Ortho(0, GL_WIDTH, 0, GL_HEIGHT, -1, 1); GL.ClearColor(Color.White); GL.Disable(EnableCap.DepthTest); //make it so that there are 50 pixels in a meter when converting ConvertUnits.SetDisplayUnitToSimUnitRatio(50f); DefaultPlayer player = new DefaultPlayer(); PhysRectangle plane = new PhysRectangle(m_PhysicsWorld, new Point(5, 10), GL_WIDTH - 20, 10); plane.m_Body.BodyType = BodyType.Static; plane.m_Body.IsStatic = true; plane.m_Body.Friction = 100.0f; PhysLine line = new PhysLine(m_PhysicsWorld, new Point(GL_WIDTH / 2 - 75, GL_HEIGHT / 2 + 100), new Point(GL_WIDTH / 2 + 50, GL_HEIGHT / 2 + 100), 40); PhysLine line2 = new PhysLine(m_PhysicsWorld, new Point(GL_WIDTH / 2 - 50, GL_HEIGHT / 2), new Point(GL_WIDTH / 2, GL_HEIGHT / 2 + 100), 30); PhysLine line3 = new PhysLine(m_PhysicsWorld, new Point(GL_WIDTH / 2 - 100, GL_HEIGHT / 2), new Point(GL_WIDTH / 2 + 100, GL_HEIGHT / 2), 10); player.drawFig = true; figureList.Add(player); figureList.Add(plane); figureList.Add(line); figureList.Add(line2); figureList.Add(line3); }
public PhysLine(World _world, Point _posA, Point _posB, float _thickness) { m_Rect = ConstructRectFromTwoPoints(_posA, _posB, _thickness); CircleShape CircleA = new CircleShape(ConvertUnits.ToSimUnits(_thickness) / 2, 10.0f); CircleShape CircleB = new CircleShape(ConvertUnits.ToSimUnits(_thickness) / 2, 10.0f); Transform rectTrans = new Transform(); m_Rect.m_Body.GetTransform(out rectTrans); //WHY rectTrans.q.Set(-rectTrans.q.GetAngle()); CircleA.Position = MathUtils.Mul(ref rectTrans.q, m_Rect.m_Body.Position - new Vector2(ConvertUnits.ToSimUnits(_posA.X), ConvertUnits.ToSimUnits(_posA.Y))); CircleB.Position = MathUtils.Mul(ref rectTrans.q, m_Rect.m_Body.Position - new Vector2(ConvertUnits.ToSimUnits(_posB.X), ConvertUnits.ToSimUnits(_posB.Y))); m_CircleA = new PhysCircle(_world, _posA, _thickness / 2, m_Rect.m_Body, CircleA); m_CircleB = new PhysCircle(_world, _posB, _thickness / 2, m_Rect.m_Body, CircleB); m_Thickness = ConvertUnits.ToSimUnits(_thickness); m_Position = new Vector2(ConvertUnits.ToSimUnits(_posA.X), ConvertUnits.ToSimUnits(_posA.Y)); m_World = _world; figColor = Color.FromArgb(255, 0, 0, 255); }