# Need help with tranlating world cords to player cords

## Recommended Posts

Yes hello. I was hoping to get some help with my game engine. I'm trying to add some collision to my game. The problem is when I try to use the code to detect the collision, BUGS. Please help please help

##### Share on other sites

What have you done to debug this?  Where does it crash in the debugger?  What compiler are you using?  What is the error message the compiler returns exactly?

Few here are going to download your code, rename it to a .c/cpp, get the required libraries you use (SFML, etc), set up the build environment, compile and debug it for you.

##### Share on other sites
It's not really a bug as maxh as it's not doing what I want.when i try to stop the player from moving when he hit a wall it doesn't work. It stop rhe player from moving in that direction. I know it's somewhere when I try to update player movement.
https://pastebin.com/mihFbL9X

##### Share on other sites

How "does it not work" ?

So the end-result is not what you want. The next question is always "why?". To answer, you go down a level.

You should have code that stops the player at the right time (if not, maybe add some code for it :P )[1]. Is that code getting called at all? Is it getting called at the right moment? Does it get the right information at the right time? If these questions are all answered with "yes" then the code itself is broken (it gets called at the right moment, it gets the right information but the end-result it should give isn't there). Ask "why" about that code then.

If the answer to one or more questions is "no", then the code to stop movement is not getting the right information, no wonder it doesn't do the right thing. Next problem is then to ask "why" about the code that is responsible for whatever is not providing the information.

The game is the same every time. Output is not correct, find code responsible for it, check its input. If that is not correct, fix the code that provides that information first. If input is correct, understand why the code is not processing it input correctly, and fix the code.

[1] Yep it happens, this week, I wondered if errors in a program got properly forwarded. It turned out I completely forgot one step in the chain! No wonder it never worked :D

Edited by Alberth

##### Share on other sites

I don't see anywhere in your code where you test against hitting a block/wall.  That's probably your biggest problem.

I notice it is possible to move both up and down, both left and right, at the same time. Is that something you really wanted?

I believe the formula your previous helper couldn't figure out is this below. It would be easier if SFML kept it in radians with the same zero value as the rest of the math world, but whatever. The dx and dy are the relative distance between the mouse and the resource. The atan2() function returns the angle based on those distances from the origin. The 180/PI is to convert from radians to degrees, and the +180 is because SFML points in a different direction than standard trigonometry.

float dx = mse_pos.x - m_res.x;
float dy = mse_pos.y - m_res.y;
float rotationAngle = ( atan2(dy,dx) * 180/PI ) + 180;


##### Share on other sites
No I just one movement at a time. I know it's not checking for walls. That's what I'm trying to do that's why I'm asking for help with translating block corrds to 2d int array corrds. The 2d int array is how I set up the 2d block array. What I need help with is translating the corrds back snd fourth so I can check for collision.

##### Share on other sites

The usual approach is for you to give it a try, and when you get stuck, show what you did, tell us what you aim for, and how the current thing is not working exactly.

Learning to program is best done by yourself !

I'd start with just translating coordinates only. Get the coordinates of the player, and translate them to world coordinates. Then take those world coordinates, and translate them back to player coordinates (which should of course result in the same coordinates you started with). I guess "player coordinates" means coordinates relative to the player? If so use (0,0,0) at first (ie exactly where the player is).

If that works, make a new player coordinate, say 1 unit in front, and do the same trick again. (Assuming you have 'sides' of your player.)

If that also works, that's a first step to collisions.

##### Share on other sites
No I just one movement at a time. I know it's not checking for walls. That's what I'm trying to do that's why I'm asking for help with translating block corrds to 2d int array corrds. The 2d int array is how I set up the 2d block array. What I need help with is translating the corrds back snd fourth so I can check for collision.

##### Share on other sites

That's what I'm trying to do that's why I'm asking for help with translating block corrds to 2d int array corrds.
Ok.

The other way around may be simpler to start with. Say I have integer coordinate (0, 0), where is that in the world. Similarly, where is (1, 0), and (2, 0), (3,0), (4,0) ...  Can you see a pattern?

##### Share on other sites
So 0,0 is = 0,0 in block corrds and 0,1 is = 0,32 and it goes from there by 32 so?

##### Share on other sites
Since your code has blocks as 32x32 objects, yes, they probably do go by 32.

This sort of conversion is usually done with a combination of multiplication, division, and modulo division, based on the conversion factors you use in your game. x/xsize and y/ysize will go one way, x*xsize and y*ysize will go the other.

##### Share on other sites

So 0,0 is = 0,0 in block corrds and 0,1 is = 0,32 and it goes from there by 32 so?

Yes 0,0 means 0,32 and goe s up 32 from there, in the block map. So I'm thinking if I have a 2d array representing the world map in main. I can update the player with the 2d array,so 0,0 would move the player to block 0,32, and so on from there

##### Share on other sites

Since your code has blocks as 32x32 objects, yes, they probably do go by 32.
This sort of conversion is usually done with a combination of multiplication, division, and modulo division, based on the conversion factors you use in your game. x/xsize and y/ysize will go one way, x*xsize and y*ysize will go the other.

Ok thanks, but I'm trying to build the game from the ground up. I at lease want ro get a programming job. So I'm thinking that as my first poke at a real project, the exprience will show my drive to get things done.

##### Share on other sites
So 0,0 is = 0,0 in block corrds and 0,1 is = 0,32 and it goes from there by 32 so?

Seems like a pattern to me. Next step where is integer coordinate (a,b) in world coordinates?

If that works, try doing it the other way around. Find a pattern with some examples, then generalize to the general case.

Edited by Alberth

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account