Editor
Joined: 3/10/2010
Posts: 899
Location: Sweden
I don't mean stability as in "won't crash", but stability as in "objects won't suddenly break the sound barrier or vibrate".
Banned User, Former player
Joined: 3/10/2004
Posts: 7698
Location: Finland
henke37 wrote:
And I am worried about performance when dealing with levels large enough to take more than a few seconds to cross.
Physics engine developers ostensibly know how to implement optimizations.
Joined: 10/20/2006
Posts: 1248
You must be thinking very highly of your coding skills if you think an engine you write all by yourself, even though it seems you hardly have any experience, is going to be more "stable" than well tested engines that many experienced people have been working on. I'm not suggesting you should use one, but your reasoning for why you don't seems questionable. Especially, since the very thing you are afraid of happening with premade engines is already happening in yours (i.e. randomly falling through the floor etc). Thanks @Derakon for adding his input, I agree. Somehow I got the impression that henke was trying to create a widely reusable, generic platforming engine. If you got the whole game planned out already, you can take many shortcuts. Although, I'd at least add comments inside the code that express the silent assumptions that it makes. (f.e. the player's speed can't exceed M, so maxSpeed must never be changed by anybody touching the code, and that walls can't be thinner than N) As a project grows, even if you are the only coder, I think you'll come to find these kinds of comments helpful. I still cringe at changing the player's coordinates before applying collision detection (I know you haven't been arguing against that). I think it's very bad practice. Since henke hasn't replied, I'll assume the worst case that he silently disagrees. Maybe this will make it more apparent why it's such a red flag to me. I read the code somewhat like this:
      protected function xMovement():void {
         horizontalControl();
         
         //cap the speed
         if(player.xSpeed>maxXSpeed) { player.xSpeed=maxXSpeed; }
         else if(player.xSpeed< -maxXSpeed) { player.xSpeed= -maxXSpeed; }
         
         if(autoMirror) {
            if(player.xSpeed<0>0) {
               player.char.scaleX=1;
            }
         }
         
         player.x+=player.xSpeed;
         //WARNING! Under no circumstances must any other method be called between these lines
         //player.x may not actually be the player's x until applyCollisions returns!
         player.applyCollisions(true,false);
      } 
      ....
      playerInternal function applyCollisions(xDir:Boolean,yDir:Boolean):void {
         //WARNING! Every method called by applyCollisions must not assume that player.x is actually the player's x or that player.y is actually the player's y
         //It might be that they indicate the player is inside a wall even though he/she/it actually isn't 
It just begs for it to eventually introduce bugs. For example, you can't even call player.isUnderWater()* which returns true if player.x and player.y correspond to coordinates that are under water during applyCollisions, unless the wall he/she/it might be temporarily inside of knows it's an underwater wall. (An exception is if water can only be at sea level and there can be no individual pools of water above that, but I hope you get the point). Do yourself a favor and don't write code like this. If you do stuff like that in more than one place, chances are you're going to have a very hard time. I think that's all I'll have to say. Good luck. *) Of course this method shouldn't really be part of the player object anyway. Just for the sake of illustration.
Editor
Joined: 3/10/2010
Posts: 899
Location: Sweden
The thing is, I know not to do that. I just don't know a good alternative.
Player (144)
Joined: 7/16/2009
Posts: 686
Assuming you can test wether any given position is "occupyable" by the player, you'd do something like this (this comes from an old game maker project of mine, so it works):
sign = sign(xSpeed);

// Test how far you can move
while (place_meeting(x + xSpeed, y, objBlock) && sign * xSpeed> 0)
{
    xSpeed -= sign;
}

// Make sure you don't move backwards
if (sign * xSpeed< 0)
    xSpeed = 0;

// Apply movement
player.x += xSpeed;
Also, I disagree with moving in both x and y at the same time. Doing that is not only harder, the seperation of the two is often preferred, to ease movements like sliding down a wall. Of course, the moment your collision surfaces are no longer just along the major axes, you need to generalize anyway.[/code]
Joined: 7/2/2007
Posts: 3960
henke37 wrote:
The thing is, I know not to do that. I just don't know a good alternative.
Use a pre-existing library. Seriously.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Editor
Joined: 3/10/2010
Posts: 899
Location: Sweden
I just want to hand this over to someone else permanently. I don't like platforming code and just want to move on to more important parts of the project.
Joined: 7/2/2007
Posts: 3960
Then use a pre-existing library. That's what they're there for! If you insist on having your own physics implementation (where by "physics" I mean the process of detecting and reacting to collisions), then I guarantee that you're going to spend a lot of time tinkering with it and trying to fix obscure bugs. I know this because I have done it. One of the reasons why Jetblade died out as a project was because I insisted on writing everything myself. Collision detection and response is a hard problem. There's very rarely a reason to solve it yourself when other people have already solved it for you. (Another reason Jetblade died is because I tried to be waaaaay too general in everything, but that's not currently relevant)
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Editor
Joined: 3/10/2010
Posts: 899
Location: Sweden
The problem is that using a library doesn't mean that the task is done for me. A library is just an aid. Not a solution. Someone still has to hook it up, deal with the resulting issues and in general tune it.
Joined: 7/2/2007
Posts: 3960
And? You'd have to do that anyway regardless of what approach you use. You're still saving a ton of time in using a pre-tested library to do a significant part of the work for you. Or hey, I bet you could buy a "platforming physics" package in Unity's store. Probably wouldn't cost you too much compared to the time you'd save, either.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.