12
lxmcf
6y

Ok here goes me trying to explain some logic here, I apologise in advance!

I've been using an axis based movement system for my games for a while now but always had the issue of characters moving faster diagonally because the movement shape would form a square; meaning things would move at twice the speed.

Only now thought 'hang on, direction's act as circles when given a radius..'

Suddenly everything works perfectly fine and all it took was 3 lines of code... Well done Alex you tool.

Comments
  • 0
    Unity? Gamemaker?
  • 1
    @CozyPlanes GameMaker my dude
  • 2
    If you don't mind, two small things from a "reviewer's perspective":

    - the inner if-block can be simplified by switching if and else (the ! Is not needed)
    - is the surrounding check really needed? (the position would be unchanged in the 0-case IISTC ). If the check really needs to be there, it could -depending on the sounding code- be possible to use an early return to reduce nesting depth ;)
  • 2
    @losdanielos the check was just to quickly test the difference between the two movement systems, it's been totally removed for the directional system now.

    And yes the outer if is needed later down the road when I add more states, also it is needed to detect when standing still and facing what direction!
  • 1
    @losdanielos as far as I remember there is no early returns in GameMaker.
  • 1
    No idea if you can add this in game maker but to move in most game engines you take the normalized direction vector and add it to the entitys original position vector. To normalize a vector all you need to do is divided each element by the magnitude, then when you are adding it to the position multiple delta time and speed. Ex (pos = pos + normalized(dir) * deltaTime * speed)
  • 1
    @hexc you dont have vectors in GameMaker so no you cant. The only objects available are gameobjects.
  • 0
    @lxmcf So c++? Hate it
  • 1
    @CozyPlanes no comment lol, never given C++ a try but I did try to learn it a while ago, couldn't wrap my head around it.

    (Plus I love GameMaker a tad too much)
  • 0
    @lxmcf
    Then what language is it?
  • 1
    @CozyPlanes proprietary one called GML
  • 0
    @lxmcf Ah, I was thinking of UE
  • 1
    @CozyPlanes oh god no, hate UE with a passion
  • 1
    @lxmcf dont go from GML to C++ directly. Its a switch you might regret because its way to different. I suggest learning C# or Java first so you get an idea of OOP languages.
  • 2
    @Codex404 oh god no wouldnt think of it, I know C# and Java fairly well, just never could get into C++
  • 1
    I am on the impression I would have used sine or cosine
  • 1
    @QCat I did actually try that, but no matter what math I put down, I would end up with unintended acceleration and the movement speed wouldn't be stable
  • 1
    You could use the math I mentioned and just applied it to seperate x/y/z variables, also a vector is just a set of numbers so if you have access to arrays you can store vectors as arrays easily enough.
Add Comment