Advertisement

Need help with tranlating world cords to player cords

Started by April 07, 2017 11:57 PM
12 comments, last by Alberth 7 years, 8 months ago
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

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.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Advertisement
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

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

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;
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.
Advertisement

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.

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.

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?

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

This topic is closed to new replies.

Advertisement