• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
P0jahn

Moving up a slope

9 posts in this topic

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.

[attachment=13765:acceptable1.png]

[attachment=13766:acceptable2.png]

[attachment=13767:acceptable3.png]

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.

[attachment=13768:notacceptable1.png]

[attachment=13769:notacceptable2.png]

 

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.

0

Share this post


Link to post
Share on other sites

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++;
	}
}
0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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;
}
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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;
                }
            }
		}
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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...

:)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0