How to stop an object from walking through a wall?

This is a discussion on How to stop an object from walking through a wall? within the C Programming forums, part of the General Programming Boards category; Hi, I'm try to make a small code that when the object moves into a wall it won't move anymore ...

  1. #1
    z14
    z14 is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    How to stop an object from walking through a wall?

    Hi,

    I'm try to make a small code that when the object moves into a wall it won't move anymore

    My code is
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <stdlib.h> //To clear the screen
    void drawplayer(int x, int y, WORD color)
    {
    	HANDLE Poutputh;
    	COORD Ppos = {x,y};
    	Poutputh = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleTextAttribute(Poutputh, color);
    	SetConsoleCursorPosition(Poutputh, Ppos);
    }
    
    int drawblocks(int bx, int by, WORD bcolor)
    {
    	HANDLE boutput;
    	COORD bpos = {bx,by};
    	boutput = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleTextAttribute(boutput, bcolor);
    	SetConsoleCursorPosition(boutput, bpos);
    }
    
    x = 7;
    y = 8;
    int KeyR = 0;
    int KeyR2 = 0;
    int KeyR3 = 0;
    int KeyR4 = 0;
    int play = 1;
    int c = 0;
    int KeyPress(int key)
    {
    	return GetAsyncKeyState(key);
    }
    
    int main()
    {
    	{
    	while(play == 1)
    	{
    	c = 0;
    	drawplayer(x, y, FOREGROUND_RED);
    	printf("@");
    	drawblocks(9, 2, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    	printf("#");
    
    	if(KeyPress(VK_UP))
    	{
    		if(KeyR == 0)
    		{
    			if(y > 0)
    			{
    			system ("cls");
    			y = y - 1;
    			KeyR = 1;
    			}
    		}
    	}
    	if(!KeyPress(VK_UP))
    	{
    		if(KeyR == 1)
    		{
    			KeyR = 0;
    		}
    	}
    	
    	if(KeyPress(VK_RIGHT))
    	{
    		if(KeyR2 == 0)
    		{
    			if(x < 79)
    			{
    			system ("cls");
    			x = x + 1;
    			KeyR2 = 1;
    			}
    		}
    	}
    	
    	if(!KeyPress(VK_RIGHT))
    	{
    		if(KeyR2 == 1)
    		{
    			KeyR2 = 0;
    		}
    	}
    
    	if(KeyPress(VK_LEFT))
    	{
    		if(KeyR3 == 0)
    		{
    			if(x > 0)
    			{
    				system ("cls");
    				x = x -1;
    				KeyR3 = 1;
    			}
    		}
    	}
    
    	if(!KeyPress(VK_LEFT))
    	{
    		if(KeyR3 == 1)
    		{
    			KeyR3 = 0;
    		}
    			if(y < 24)
    			{
    				system ("cls");
    	}
    
    	if(KeyPress(VK_DOWN))
    	{
    		if(KeyR4 == 0)
    		{
    				y = y + 1;
    				KeyR4 = 1;
    			}
    		}
    	}
    
    	if(!KeyPress(VK_DOWN))
    	{
    		if(KeyR4 == 1)
    		{
    			KeyR4 = 0;
    		}
    	}
    
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Well first I would recommend you to use OpenGL for such things, well about the question it self, you can imagine your object is inside a box/sphere so you can check its lengh/2 or radius and see if it touches the wall. I got a function that stops an air plane to cross the floor on a opengl project I can post if you wanna check but the basic idea is you need to define a plane and check collision against it.

  3. #3
    z14
    z14 is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    My program runs in dos, I don't think you can run OPENGL in Dos.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,507
    > if(!KeyPress(VK_LEFT))
    After this point, the braces and indentation are all wrong.

  5. #5
    z14
    z14 is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    Quote Originally Posted by Salem
    > if(!KeyPress(VK_LEFT))
    After this point, the braces and indentation are all wrong.
    Yes I know, that's what stops my object from moving when a key is pressed, but I'm trying to figure out how to stop my object from going through a wall in my program.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,507
    Better use of conditions like
    if(y < 24)
    would seem to be the way to go.

    Or you could have a map of the screen, say
    char map[80][24];

    And tests like
    if ( map[x][y+1] != '#' ) y = y + 1;

    That is, you only move to a position if there is no wall there.
    Extend this idea to cover other things you can't move into, say monsters, or things you can pick up, say food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. Replies: 4
    Last Post: 11-14-2006, 10:52 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21