I am trying to wrap my head around some concepts in a side project I'm working on. I'm struggling with how I can calculate a vector based on my character movement logic. Here are the relevant details of my character movement:
- The character's hit box for collisions (and map position validity) is defined as a rectangle: x,y,w,h
A character can move in 8 directions, let's keep this simple to ignore strafing, call this
direction
:- Left
(x-1)
, Right(x+1)
, Up(y-1)
, Down(y+1)
- UpLeft
(x-1,y-1)
, UpRight(x+1,y-1)
,DownLeft(x-1,y+1)
, DownRight(x+1,y+1)
- Left
The character can be in two moving states, again keeping numbers simple:
- Walking
1 x offsets
- Running
2 x offsets
- Walking
When a character is moved the following logic happens:
- Create their proposed new hit box by adding an offset to their x & y coordinates based on their
direction
, call thisproposedHitBox
- Check if the
proposedHitBox
is a valid position on the map, if so, then update the character's position to the location defined on theproposedHitBox
- If the
proposedHitBox
is not a valid location on the map it should try to glance the character. This is where I'm having trouble.
- Create their proposed new hit box by adding an offset to their x & y coordinates based on their
Given all this, how do I essentially, calculate a vector of their proposed motion?
Example:
- Character is at 10,10.
- Character starts running.
- Input is
DownLeft
. - Proposed location is 8,12
(x-(1x2),y+(1x2))
. - Proposed location is valid.
- Update character position to 8,12.
- What calculation defines my vector? How does magnitude come from this? Is that generally stored as Z in a 2D game? So I'd use a Vector3 to store it? How does velocity come into this?
I'd assume my vector is a 45deg angle from current to proposed...?
Edit
Here is a picture of what the walls can look like in the game. The red box being the hit box as discussed above. The character is bound inside the blue polygon (which represents outer walls). Black being inner walls. The way I was doing glancing was overlaying 4x4 tiles across this entire map and then for each tile that the character should glance on I would choose an incoming direction
(i.e. DownLeft
) and then redirect them to a different direction
(i.e. Down
) which should make the proposed hit box in a valid position. This is however very tedious as I have to create glances for multiple directions for every tile. I'm looking for hopefully a better way.
I was reading that you could add vectors to each tile but I'm not sure how this works because on some tiles a incoming direction
does not always have the same out direction
.