basic problem for world space to object space

Started by
4 comments, last by Buckeye 13 years, 3 months ago
Hi there!

I have a simple 2D problem.

- In my game i have a visual grid of variable size.
- I have objects represented by points in the grid cells.
- The grid can be rotated clockwise or anti clockwise with variable rotation.

My goal is to determine the position(row,col) of a given point in the grid.
Here is how i achieve it when their is no rotation (using world space only):

row_number = ((myPoint.y - myGridMinX) / cellSize).ceiling
col_number = ((myPoint.x - myGridMinY) / cellSize).ceiling

Its easy but as soon as its rotated the above code is not working anymore.
I can't figure out how to convert my calculation in the grid's space.

Now, let say i have a grid of 4 x 3 rotated 45 degrees anti clockwise, how should i calculate my point position?
Thank you.
Advertisement
You don't mention what API* you're using (if that's important), but you can use vector-matrix multiplication. I.e., google for "vector matrix multiplication." You'll need to construct a matrix representing the translation/rotation of your grid and multiply each of the coordinates by that matrix.

*Many APIs provide functions to do what you want. E.g., in DirectX, there's a D3DXVec3TransformCoord function that would serve.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Hi, thx for the quick reply,

"You'll need to construct a matrix representing the translation/rotation of your grid"

This is exactly what i can't figure out, i understand the matrix multiplication with each of my coordinates concept, but how to construct them?
Could you give me an example if i have a 2x2 grid rotated a 45 degree and where the lower left corner is at (2,1) in world space?
I am using Irrlicht, but its not an API concern...

You don't mention what API* you're using (if that's important), but you can use vector-matrix multiplication. I.e., google for "vector matrix multiplication." You'll need to construct a matrix representing the translation/rotation of your grid and multiply each of the coordinates by that matrix.

*Many APIs provide functions to do what you want. E.g., in DirectX, there's a D3DXVec3TransformCoord function that would serve.
Just to elaborate a bit, if you have a point in world space and want to use the equations you posted to compute the corresponding grid point, you'll first want to transform the world-space point by the inverse of the world transform for the grid.

As for how to build these various transforms, that's just basic 3-d math, so any good reference on that topic should be able to help you with that.

What math library are you using?
Ok...
I am not using any right now.
Thx.

Just to elaborate a bit, if you have a point in world space and want to use the equations you posted to compute the corresponding grid point, you'll first want to transform the world-space point by the inverse of the world transform for the grid.

As for how to build these various transforms, that's just basic 3-d math, so any good reference on that topic should be able to help you with that.

What math library are you using?

Hi, thx for the quick reply,

"You'll need to construct a matrix representing the translation/rotation of your grid"

This is exactly what i can't figure out, i understand the matrix multiplication with each of my coordinates concept, but how to construct them?
Could you give me an example if i have a 2x2 grid rotated a 45 degree and where the lower left corner is at (2,1) in world space?
I am using Irrlicht, but its not an API concern...


It's definitely an API concern. I'm not an Irrlicht person, but I'm quite certain it has the functions you need - matrix::setRotation, etc. Take a look at the API documentation and google a bit for irrlicht info, tutorials, etc.

You're eventually going to need a lot of vector and matrix manipulation and you should familiarize yourself with the capabilities of your API.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement