Pedro Pedro - 5 days ago 5
C# Question

OpenTK Y is completely inverted

Hello I am using c# and OpenTK.

For some reason i have my y completely inverted when using the mouse i need to set y negative to be right, but when i am rendering a map the map is inverted i have been trying to find the problem but i cant find what makes the y be inverted.

I will be letting a link for the c# project if the problem is not at the code below.

Game.cs where the on render, on load or on update funcions ate

using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Drawing;

namespace PlatformGame
{
class Game : GameWindow
{

public static int GridSize = 32, TileSize = 1024;

Texture2D Texture, TileSet;
Texture2D Water;
Texture2D Grass;

View view;

Level level;

public Game(int width,int height)
: base(width,height)
{
GL.Enable(EnableCap.Texture2D);

view = new View(Vector2.Zero, 1.0, 0.0);

Input.Initialize(this);
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

Water = ContentPipe.LoadTexture("Water.png");
Grass = ContentPipe.LoadTexture("Grass.png");
TileSet = ContentPipe.LoadTexture("Tiles.png");
level = new Level("Content/Level.tmx");
}

protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);

if(Input.MousePress(OpenTK.Input.MouseButton.Left))
{
Vector2 pos = new Vector2(Mouse.X, Mouse.Y) - new Vector2(Width, Height) / 2f;
pos = view.ToWorld(pos);
view.SetPosition(pos, View.TweenType.QuarticOut, 15);
}

if (Input.Keydown(OpenTK.Input.Key.Right))
{
view.SetPosition(view.PositionGoTo + new Vector2(5, 0), View.TweenType.QuarticOut, 15);
}

if (Input.Keydown(OpenTK.Input.Key.Left))
{
view.SetPosition(view.PositionGoTo + new Vector2(-5, 0), View.TweenType.QuarticOut, 15);
}

if (Input.Keydown(OpenTK.Input.Key.Up))
{
view.SetPosition(view.PositionGoTo + new Vector2(0, 5), View.TweenType.QuarticOut, 15);
}

if (Input.Keydown(OpenTK.Input.Key.Down))
{
view.SetPosition(view.PositionGoTo + new Vector2(0, -5), View.TweenType.QuarticOut, 15);
}

view.Update();
Input.Update();

}

protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit); //Clear Window
GL.ClearColor(Color.CornflowerBlue); //Set Windo background to Corn flower blue

SpriteBatch.Begin(Width, Height);
view.ApplyTransform();

for (int x = 0; x < level.Width; x++)
{
for (int y = 0; y < level.Height; y++)
{
RectangleF Source = new RectangleF(0, 0, 0, 0);

switch (level[x,y].Type)
{
case BlockType.Ladder:
Source = new RectangleF(0 * TileSize, 2 * TileSize, TileSize, TileSize);
break;
case BlockType.Water:
Source = new RectangleF(0 * TileSize, 0 * TileSize, TileSize, TileSize);
break;
case BlockType.Stone:
Source = new RectangleF(0 * TileSize, 1 * TileSize, TileSize, TileSize);
break;
case BlockType.Platform:
Source = new RectangleF(1 * TileSize, 1 * TileSize, TileSize, TileSize);
break;
case BlockType.Dirt:
Source = new RectangleF(2 * TileSize, 0 * TileSize, TileSize, TileSize);
break;
case BlockType.Grass:
Source = new RectangleF(1 * TileSize, 0 * TileSize, TileSize, TileSize);
break;
case BlockType.LadderPlatform:
Source = new RectangleF(2 * TileSize, 1 * TileSize, TileSize, TileSize);
break;
}

SpriteBatch.Draw(TileSet, new Vector2(x * GridSize, y * GridSize), new Vector2((float)GridSize / TileSize), Color.Transparent, Vector2.Zero, Source);
}
}

SwapBuffers();
}
}
}


View.cs what controls the camera

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace PlatformGame
{
class View
{

public enum TweenType
{
Instante,
Linear,
QuadraticInOut,
CubicInOut,
QuarticOut,
}

private Vector2 position;
/// <summary>
/// In radians, + = clockwise
/// </summary>
public double rotation;
/// <summary>
/// 1 = no zoom
/// 2 = 2x zoom
/// </summary>
public double zoom;

private Vector2 PositionGoto, PositionFrom;
private TweenType tweenType;
private int CurrentStep, TweenStep;

public Vector2 Position
{
get
{
return position;
}
}
public Vector2 PositionGoTo
{

get { return PositionGoto; }
}

public Vector2 ToWorld(Vector2 input)
{
input /= (float)zoom;
Vector2 dX = new Vector2((float)Math.Cos(rotation),(float)Math.Sin(rotation));
Vector2 dY = new Vector2((float)Math.Cos(rotation + MathHelper.PiOver2), (float)Math.Sin(rotation + -MathHelper.PiOver2));

return (position + dX * input.X + dY * input.Y);
}

public View(Vector2 StartPosition,double StartZoom = 1.0, double StartRotation = 0.0)
{
position = StartPosition;
zoom = StartZoom;
rotation = StartRotation;
}

public void Update()
{
if (CurrentStep < TweenStep)
{
CurrentStep++;
switch (tweenType)
{
case TweenType.Linear:
position = PositionFrom + (PositionGoto - PositionFrom) * GetLinear((float)CurrentStep / TweenStep);
break;
case TweenType.QuadraticInOut:
position = PositionFrom + (PositionGoto - PositionFrom) * GetQuadraticInOut((float)CurrentStep / TweenStep);
break;
case TweenType.CubicInOut:
position = PositionFrom + (PositionGoto - PositionFrom) * GetCubicInOut((float)CurrentStep / TweenStep);
break;
case TweenType.QuarticOut:
position = PositionFrom + (PositionGoto - PositionFrom) * GetQuarticOut((float)CurrentStep / TweenStep);
break;
}


}
else
{
position = PositionGoto;
}
}

public void SetPosition(Vector2 NewPosition)
{
position = NewPosition;
PositionFrom = NewPosition;
PositionGoto = NewPosition;
tweenType = TweenType.Instante;
CurrentStep = 0;
TweenStep = 0;
}

public void SetPosition(Vector2 NewPosition, TweenType type, int NumSteps)
{
PositionFrom = position;
position = NewPosition;
PositionGoto = NewPosition;
tweenType = type;
CurrentStep = 0;
TweenStep = NumSteps;
}

public float GetLinear(float t)
{
return t;
}

public float GetQuadraticInOut(float t)
{
return (t * t) / ((2 * t * t) - (2 * t) + 1);
}

public float GetCubicInOut(float t)
{
return (t * t * t) / ((3 * t * t) - (3 * t) + 1);
}

public float GetQuarticOut(float t)
{
return -((t - 1) * (t - 1) * (t - 1) * (t - 1)) + 1;
}

public void ApplyTransform()
{
Matrix4 transfotm = Matrix4.Identity;

transfotm = Matrix4.Mult(transfotm, Matrix4.CreateTranslation(-position.X, -position.Y, 0));
transfotm = Matrix4.Mult(transfotm, Matrix4.CreateRotationZ(-(float)rotation));
transfotm = Matrix4.Mult(transfotm, Matrix4.CreateScale((float)zoom, (float)zoom, 1.0f));

GL.MultMatrix(ref transfotm);
}
}
}


Project Link

Answer

I have found the problem the the problem is in the spritebatch.cs

it have a line like this:

GL.Ortho(-ScreenWidth / 2f, ScreenWidth / 2f, -ScreenHeight / 2f, ScreenHeight / 2f, 0f, 1f);

it is suppose to be like this

GL.Ortho(-ScreenWidth / 2f, ScreenWidth / 2f, ScreenHeight / 2f, -ScreenHeight / 2f, 0f, 1f); 
Comments