Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: A Project Of Mine (With a Bug)  (Read 1169 times)

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
A Project Of Mine (With a Bug)
« on: February 27, 2013, 07:50:35 pm »

I am making a game in C++;
And I ran into a slight problem with it, as would be expected, but I can't figure it out.

*SOLVED*
Pretty much, each square meter is divided into a 100 by 100 pixel area. Each of those areas has its own temperature and whatnot. The gases move based on the temperatures, from higher to lower.

Collision checking is only done inside of each area, to make the game run faster (not really an issue right now, but). Once it leaves an area, it is supposed to transfer to the area it just entered. However, it isn't doing this.

*NOT SOLVED*
Objects move 5 times too slow.

Sorry about the strange formatting;

Code: [Select]
float time_change = delta_time / 1000.0f;
// stuff
this->x += this->x_vel * time_change;
this->y += this->y_vel * time_change;

            if (((int)(this->x / 100) != this->area_x) && ((int)(this->y / 100) != this->area_y)) //if no longer in the same area, move it
            {
                transfer = modify.area[this->area_x][this->area_y].all[this->ID];
                modify.area[this->area_x][this->area_y].all.erase(modify.area[this->area_x][this->area_y].all.begin() + this->ID);

                this->area_x = this->x / 100;
                this->area_y = this->y / 100;

                if (((this->area_x >= 0) && (this->area_x < 15)) && ((this->area_y >= 0) && (this->area_y < 15)))
                {
                    this->ID = modify.area[this->area_x][this->area_y].all.size();
                    modify.area[this->area_x][this->area_y].all.push_back(transfer);
                }
                else
                {
                    //move area
                }
            }

                if ((this->area_x >= 0) && (this->area_x < 15))
                {
                    if ((this->area_y >= 0) && (this->area_y < 15))
                    {
                        this->ID = modify.area[this->area_x][this->area_y].all.size();
                        modify.area[this->area_x][this->area_y].all.push_back(transfer);
                    }
                }
            }

UPDATE 1: It likely has something to do with time_change, because if I "hold" the window, therefore increasing the time_change, it does collide properly with the object, which means it properly transferred areas.
In the same area is that objects generally move 5 times too slow, for some unknown reason.

Thanks in advance.
« Last Edit: February 28, 2013, 03:34:02 pm by kytuzian »
Logged

forsaken1111

  • Bay Watcher
    • View Profile
    • TTB Twitch
Re: A Project Of Mine (With a Bug)
« Reply #1 on: February 27, 2013, 07:58:41 pm »

Don't put code tags in spoilers. They self-limit the length of the code tag anyway but putting em in spoilers just makes it hard to read.
Logged

GalenEvil

  • Bay Watcher
    • View Profile
    • Mac-Man Games
Re: A Project Of Mine (With a Bug)
« Reply #2 on: February 27, 2013, 08:41:15 pm »

I think that this is where the problem starts:
Code: [Select]
this->area_x = this->x / 100;
 this->area_y = this->y / 100;
I am not super literate in C++ but that is an integer division which means that it will produce an integer result (in most if not all languages). Turn it into 100.0 for each to make it a non-integer division and see if that fixes the problem :D
Logged
Fun is Fun......Done is Done... or is that Done is !!FUN!!?
Quote from: Mr Frog
Digging's a lot like surgery, see -- you grab the sharp thing and then drive the sharp end of the sharp thing in as hard as you can and then stuff goes flying and then stuff falls out and then there's a big hole and you're done. I kinda wish there was more screaming, but rocks don't hurt so I guess it can't be helped.

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: A Project Of Mine (With a Bug)
« Reply #3 on: February 27, 2013, 09:10:25 pm »

I think that this is where the problem starts:
Code: [Select]
this->area_x = this->x / 100;
 this->area_y = this->y / 100;
I am not super literate in C++ but that is an integer division which means that it will produce an integer result (in most if not all languages). Turn it into 100.0 for each to make it a non-integer division and see if that fixes the problem :D

Well, area_x and area_y are both integers. It is intentionally integer division, because integer division always rounds down, which is favorable in this situation. Therefore just moving slightly (from x = 100 to x = 99) would cause it to go from area_x = 1 to area_x = 0. At least that's the idea.

Also,

Code: [Select]
if (((int)(this->x / 100) != this->area_x) && ((int)(this->y / 100) != this->area_y))

its actually not detecting its move in the first place.

Thanks for answering though.
« Last Edit: February 27, 2013, 09:16:01 pm by kytuzian »
Logged

GalenEvil

  • Bay Watcher
    • View Profile
    • Mac-Man Games
Re: A Project Of Mine (With a Bug)
« Reply #4 on: February 27, 2013, 11:14:52 pm »

okay, thanks for the information :D Didn't know the type of area_[x/y] hehe
Logged
Fun is Fun......Done is Done... or is that Done is !!FUN!!?
Quote from: Mr Frog
Digging's a lot like surgery, see -- you grab the sharp thing and then drive the sharp end of the sharp thing in as hard as you can and then stuff goes flying and then stuff falls out and then there's a big hole and you're done. I kinda wish there was more screaming, but rocks don't hurt so I guess it can't be helped.

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: A Project Of Mine (With a Bug)
« Reply #5 on: February 28, 2013, 01:14:48 am »

Correct me if I'm wrong, but doesn't && denote an and? (I prefer to just write and) If so, then it only notices diagonal movement. Just replace it with an or, and it should work.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: A Project Of Mine (With a Bug)
« Reply #6 on: February 28, 2013, 07:07:32 am »

Correct me if I'm wrong, but doesn't && denote an and? (I prefer to just write and) If so, then it only notices diagonal movement. Just replace it with an or, and it should work.

*facepalm*

You're right, thanks.
Well damn, that was easy, and made me feel like an idiot. At least it got solved.

All that's left is the reason why everything moves 5 times too slow.

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: A Project Of Mine (With a Bug)
« Reply #7 on: February 28, 2013, 10:51:39 am »

On the processing side, or on the actual movement itself? And compared to what?

You could simply make things go 5 times faster by dividing the time by 200 rather than by 1000. Or is that just sidestepping the problem?

And may I ask, whats with all the "this->"? Unless you have a particularly weird compiler, the this-> is automatically added(or an equivalent or whatever, point is that it knows the variables of the class).
« Last Edit: February 28, 2013, 10:53:16 am by cerapa »
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: A Project Of Mine (With a Bug)
« Reply #8 on: February 28, 2013, 03:32:55 pm »

On the processing side, or on the actual movement itself? And compared to what?

You could simply make things go 5 times faster by dividing the time by 200 rather than by 1000. Or is that just sidestepping the problem?

And may I ask, whats with all the "this->"? Unless you have a particularly weird compiler, the this-> is automatically added(or an equivalent or whatever, point is that it knows the variables of the class).

The movement itself. If x_vel was 10 it would only move about 2 pixels per second. I've tried doing some like dividing by 200 ( I multiplied x_vel by 5 ), and its not exact, so its mostly just sidestepping.

I've just always used this->, though the compiler I use does it without it. Its just a habit since one time I added an argument to a function with the same name as a member variable, so it didn't work right. Not really any reason, since I try to make sure I never do that anymore.