I’m pretty new to SDL so this may be a really obvious question, but I was wondering how I could get the mouse position relative to the game world? SDL_GetMouseState(x,y) returns the position relative to the window.
If there’s not a function, is there some math formula I could use?
2D or 3D? Please provide some more detail about what you’re trying to do.
It’s 2D. I need to figure out the angle that a bullet sprite should be set to in order to move in the proper direction. The player aims with the mouse, and I need to find the distance between the player’s sprite and the cursor in order to get the proper angle.
Just a few minutes ago, I did realize that since the player’s sprite is always in the very center of the screen, I could simply check the distance from the mouse to the center of the screen.
Here’s a screenshot using the above method. It doesn’t work perfectly just yet, but I think I know how to fix it.
I have some classes in my engine to help with that.
The basic formula you can use is something like.
mouse_v_pos = Matrix::Transform(mouse_real_pos,Matrix::Invert(get_camera_transformation()))
mouse_v_pos is a Vector2, pretty much a class with X, Y. Matrix is the class that holds transformations.
To generate your camera Matrix
Matrix get_camera_transformation()
{
return
Matrix::CreateTranslation(pos.X,-pos.Y, 0) *
Matrix::CreateScale(camera_zoom, camera_zoom, 1)*
Matrix::CreateTranslation(camera_origin.X,camera_origin.Y, 0);
}
Matrix functions needed
class Matrix
{
public:
Matrix(void){ }
virtual ~Matrix(void){ }
public:
float M11;
float M12;
float M13;
float M14;
float M21;
float M22;
float M23;
float M24;
float M31;
float M32;
float M33;
float M34;
float M41;
float M42;
float M43;
float M44;
static Matrix Identity()
{
return Matrix(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
static Matrix CreateTranslation(const float xPosition, const float yPosition, const float zPosition)
{
Matrix returnMatrix = Matrix::Identity();
returnMatrix.M41 = xPosition;
returnMatrix.M42 = yPosition;
returnMatrix.M43 = zPosition;
return returnMatrix;
}
static Matrix CreateScale(float scale)
{
Matrix returnMatrix = Matrix::Identity();
returnMatrix.M11 = scale;
returnMatrix.M22 = scale;
returnMatrix.M33 = scale;
return returnMatrix;
}
static Matrix Multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
result.M11 = matrix1.M11 * matrix2.M11 + matrix1.M12 * matrix2.M21 + matrix1.M13 * matrix2.M31 + matrix1.M14 * matrix2.M41;
result.M12 = matrix1.M11 * matrix2.M12 + matrix1.M12 * matrix2.M22 + matrix1.M13 * matrix2.M32 + matrix1.M14 * matrix2.M42;
result.M13 = matrix1.M11 * matrix2.M13 + matrix1.M12 * matrix2.M23 + matrix1.M13 * matrix2.M33 + matrix1.M14 * matrix2.M43;
result.M14 = matrix1.M11 * matrix2.M14 + matrix1.M12 * matrix2.M24 + matrix1.M13 * matrix2.M34 + matrix1.M14 * matrix2.M44;
result.M21 = matrix1.M21 * matrix2.M11 + matrix1.M22 * matrix2.M21 + matrix1.M23 * matrix2.M31 + matrix1.M24 * matrix2.M41;
result.M22 = matrix1.M21 * matrix2.M12 + matrix1.M22 * matrix2.M22 + matrix1.M23 * matrix2.M32 + matrix1.M24 * matrix2.M42;
result.M23 = matrix1.M21 * matrix2.M13 + matrix1.M22 * matrix2.M23 + matrix1.M23 * matrix2.M33 + matrix1.M24 * matrix2.M43;
result.M24 = matrix1.M21 * matrix2.M14 + matrix1.M22 * matrix2.M24 + matrix1.M23 * matrix2.M34 + matrix1.M24 * matrix2.M44;
result.M31 = matrix1.M31 * matrix2.M11 + matrix1.M32 * matrix2.M21 + matrix1.M33 * matrix2.M31 + matrix1.M34 * matrix2.M41;
result.M32 = matrix1.M31 * matrix2.M12 + matrix1.M32 * matrix2.M22 + matrix1.M33 * matrix2.M32 + matrix1.M34 * matrix2.M42;
result.M33 = matrix1.M31 * matrix2.M13 + matrix1.M32 * matrix2.M23 + matrix1.M33 * matrix2.M33 + matrix1.M34 * matrix2.M43;
result.M34 = matrix1.M31 * matrix2.M14 + matrix1.M32 * matrix2.M24 + matrix1.M33 * matrix2.M34 + matrix1.M34 * matrix2.M44;
result.M41 = matrix1.M41 * matrix2.M11 + matrix1.M42 * matrix2.M21 + matrix1.M43 * matrix2.M31 + matrix1.M44 * matrix2.M41;
result.M42 = matrix1.M41 * matrix2.M12 + matrix1.M42 * matrix2.M22 + matrix1.M43 * matrix2.M32 + matrix1.M44 * matrix2.M42;
result.M43 = matrix1.M41 * matrix2.M13 + matrix1.M42 * matrix2.M23 + matrix1.M43 * matrix2.M33 + matrix1.M44 * matrix2.M43;
result.M44 = matrix1.M41 * matrix2.M14 + matrix1.M42 * matrix2.M24 + matrix1.M43 * matrix2.M34 + matrix1.M44 * matrix2.M44;
return result;
}
static Vector2 Transform(const Vector2& position, const Matrix& matrix)
{
return Vector2((position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41,
(position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42);
}
Matrix operator *(Matrix matrix)
{
return Matrix::Multiply(*this, matrix);
}
}
I hope this helps