• FEATURED

View more

View more

View more

\$30

### Image of the Day Submit

IOTD | Top Screenshots

## Moving up a slope

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1P0jahn  Members

Posted 20 February 2013 - 04:21 PM

EDIT: Problem solved

I have made a lot of progress in my game recently. One thing that I need help with is moving up a slope.

In my game, the tiles are pixels, stored as an 2d array char: 0 is hollow, 1 is solid.

Look at these pictures below, the brown square are solid(1) and the gray is hollow(0). The black grids are pixels.

In these three cases, I would move up when standing in front of the slope.

In these two cases, it would be threated as a wall, because the slope is two pixels high, anything that exceeds one count as a wall.

I implemented this before. All I did was to check if there was an slope in front of me, and it were, increased the height of the character. This did not work well with the running mechanism. When I start run faster, I do not increase one step per frame, it could be like 5 pixels per frame.

So the slope-check always failed in high speed. Even when it worked,

This is the piece of code that executes when the left left key is down

        if(the left key is down)
{
if(vx < maxX) //Check so I dont surpass the max speed.
vx += accX * DELTA;

float nextX = currX - vx * DELTA;
if(canGoToLeft(nextX))  //Test if I can go to the possible next X position.
moveTo(nextX, currY);
else //I have most likely ran into an wall, so set vx to 0.
vx = 0;
}


The one for right key is similar.

### #2zacaj  Members

Posted 20 February 2013 - 04:44 PM

Couldn't you just iterate one pixel at a time, adjusting the y if its a slope, and if you hit a wall not moving the whole 5?

if(left)
{
if(vx < maxX) //Check so I dont surpass the max speed.
vx += accX * DELTA;

float nextX = currX - vx * DELTA;
for(;currX>=nextX;currX--)
{
if(notSolidAt(currX,currY+2))
{
vx=0;
break;
}
if(notSolidAt(currX,currY+1))
currY++;
}
}


### #3L. Spiro  Members

Posted 20 February 2013 - 07:05 PM

2D games are usually tile-based.  If your game resembles Mario, you can easily store slope information for each tile and perform your tests once-per-tile instead of once-per-pixel, and on a line-segment.  If your character can never move farther than a full tile in a frame then your problem is solved.  Otherwise you can just check all the tiles the player crossed for the first slope that is not passable.

L. Spiro

### #4BeerNutts  Members

Posted 21 February 2013 - 01:40 PM

Can I also suggest not to have 2 different functions for moving left or right?  Going left simply means your velocity is negative, and going right only means the velocity is positive.  I would think a simple move function would be better than handles both left and right when given a velocity.

IMO It will make life simpler in the long run.

if (abs(vx) < maxX) {
if (LeftKeyPressed()) {
vx += -accx * DELTA;
}
else if (RightKeyPressed()) {
vx += accx * DELTA;
}
}

float nextX = currX + vx * DELTA;

if (canMoveHorizontal(nextX)) {
moveTo(nextX, currY);
}
else {
vx = 0;
}


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #5P0jahn  Members

Posted 23 February 2013 - 03:03 PM

zacajs suggestion is the most suitable for my engine. I implemented it and it worked fine, however, a bug was born during the implementation.

First, I want to say that the origin is the top-left corner. X is increasing when moving right, and Y is increasing when moving down. Negative values do not exist(index out of bounds!).

This bug occurs when targetX >= currX. The bug effect ingame is that the character is standing still until currX > targetX.

This bug get triggered when you are moving to the right and then immediately switch to the left before the character get a chance to stop, or, if you are standing still and then press left key(in this case, the freeze is short).

        if(leftInput)
{
if(vx < maxX)
vx += accX * DELTA + boostX;

float targetX = currX - vx * DELTA;

for(float next = currX; next >= targetX; next--)
{
if(canGoToLeft(next))
moveTo(next, currY);
else if(canGoToSlopeLeft(next))
moveTo(next, currY - 1);
else
{
vx = 0;
break;
}
}
}


        else if(rightInput)
{
if(-vx < maxX)
vx -= accX * DELTA + boostX;

float nextX = currX - vx * DELTA;

...
}


I know the problem lies in the for loop, but I cant figure out how to fix it.

Edited by P0jahn, 23 February 2013 - 03:04 PM.

### #6zacaj  Members

Posted 23 February 2013 - 03:14 PM

Try this:

	if(leftInput)
{
if(vx < maxX)
vx += accX * DELTA + boostX;
}
else if(rightInput)
{
if(-vx < maxX)
vx -= accX * DELTA + boostX;
}
float targetX = currX - vx * DELTA;
if(targetX<currX)
{
for(float next = currX; next >= targetX; next--)
{
if(canGoToLeft(next))
moveTo(next, currY);
else if(canGoToSlopeLeft(next))
moveTo(next, currY - 1);
else
{
vx = 0;
break;
}
}
}
else
{
for(float next = currX; next <=targetX; next++)
{
if(canGoToLeft(next))
moveTo(next, currY);
else if(canGoToSlopeLeft(next))
moveTo(next, currY - 1);
else
{
vx = 0;
break;
}
}
}


### #7P0jahn  Members

Posted 23 February 2013 - 06:22 PM

That code allowed me to walk up from slope when moving right(with some other modifications as well), but it did not fix the bug.

            if(leftInput)
{
if(vx < maxX)
vx += accX * DELTA + boostX;
}
else
{
if(-vx < maxX)
vx -= accX * DELTA + boostX;
}

float targetX = currX - vx * DELTA;

//Debugging
System.out.println("target: " + targetX + "\ncurrX:  " + currX + "\n" + vx + "\n");

if(targetX < currX)
{
for(float next = currX; next >= targetX; next--)
{
if(canGoToLeft(next))
moveTo(next, currY);
else if(canGoToSlopeLeft(next))
moveTo(next, currY - 1);
else
{
vx = 0;
break;
}
}
}
else
{
for(float next = currX; next <= targetX; next++)
{
if(canGoToRight(next))
moveTo(next, currY);
else if(canGoToSlopeRight(next))
moveTo(next, currY - 1);
else
{
vx = 0;
break;
}
}
}


So, I am standing still, vx is 0. Now I push the left arrow, the results:

target: 556.05554
currX:  556.1111
vx:     3.3333335

target: 556.0
currX:  556.1111
vx:     6.666667

target: 555.9444
currX:  556.1111
vx:     10.0

target: 555.88885
currX:  556.1111
vx:     13.333334

target: 555.8333
currX:  556.1111
vx:     16.666668

target: 555.7778
currX:  556.1111
vx:     20.000002

target: 555.72217
currX:  556.1111
vx:     23.333336

target: 555.6666
currX:  556.1111
vx:     26.66667

target: 555.6111
currX:  556.1111
vx:     30.000004

target: 555.55554
currX:  556.1111
vx:     33.333336

target: 555.5
currX:  556.1111
vx:     36.666668

target: 555.4444
currX:  556.1111
vx:     40.0

target: 555.38885
currX:  556.1111
vx:     43.333332

target: 555.3333
currX:  556.1111
vx:     46.666664

target: 555.2778
currX:  556.1111
vx:     49.999996

target: 555.22217
currX:  556.1111
vx:     53.33333

target: 555.1666
currX:  556.1111
vx:     56.66666

target: 555.1111
currX:  556.1111
vx:     59.999992

target: 554.05554
currX:  555.1111
vx:     63.333324

target: 553.0
currX:  554.1111
vx:     66.66666

target: 551.9444
currX:  553.1111
vx:     69.99999

target: 550.88885
currX:  552.1111
vx:     73.33333

target: 549.8333
currX:  551.1111
vx:     76.666664

target: 548.7778
currX:  550.1111
vx:     80.0

target: 547.72217
currX:  549.1111
vx:     83.333336

target: 546.6666
currX:  548.1111
vx:     86.66667

target: 545.6111
currX:  547.1111
vx:     90.00001

target: 544.55554
currX:  546.1111
vx:     93.33334

target: 543.5
currX:  545.1111
vx:     96.66668

target: 542.4444
currX:  544.1111
vx:     100.000015

target: 541.38885
currX:  543.1111
vx:     103.33335

target: 540.3333
currX:  542.1111
vx:     106.66669

target: 539.2778
currX:  541.1111
vx:     110.00002

target: 538.22217
currX:  540.1111
vx:     113.33336

target: 537.1666
currX:  539.1111
vx:     116.666695

target: 536.1111
currX:  538.1111
vx:     120.00003


Edited by P0jahn, 23 February 2013 - 06:23 PM.

### #8zacaj  Members

Posted 23 February 2013 - 06:50 PM

The problem is that for a while your target is less than one pixel away from currX, so the loop doesn't activate for a while.  I'd suggest you give vx an initial boost when the arrow keys are first pressed

### #9P0jahn  Members

Posted 24 February 2013 - 08:47 AM

I added a check to see if currX as an int is equal to targetX as an int, and if they are, set currX to targetX.

It didnt solve the problem.

Its very odd, because this bug did not occur with the code used in the first post. And I dont see much difference between the two variants.

Setting boostX to 1 or higher(was 0 previously) did not change it either.

### #10irbaboon  Members

Posted 28 February 2013 - 06:11 AM

Haha, your collision detection is very similar to one I made for a sidescroller. Although I did it a bit differently.

Have you considered making negative numbers 'hollow' and positive numbers 'solid' ?

In my editor, I basically made lines that make the ground. I could make different 'bodies' (or 'grounds') and it will just save the points of the lines in a file. It will then load the points and make a 2D char array which is a '0' and a '1' for solid and hollow, BUT it converts it to a 1D array with integers where a negative number signifies hollowness and a positive number signifies solid...ness...

I don't know if this will fit in your game, but if you want to know more/look at the code then just tell me.

I solved the 'more than one pixel' problem by iterating 'walk speed' amount of times.

I'm actually still trying to compress it, since right now it still needs an integer per row of pixels. I want a huge empty area to only take 4 bytes...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.