Computer Science Canada

Wall Help

Author:  Boiq [ Fri Sep 05, 2008 10:45 am ]
Post subject:  Wall Help

Okay, i'm currently working on a game programmed by Turing.

I recently added some basic graphic walls using 'drawfillbox'. I have an animated object that is being controll, so how would I make it so when it runs into the wall it stops rather then going through it.

All help would be appreciated to guide me onto the right path, thanks.

Author:  Tony [ Fri Sep 05, 2008 11:11 am ]
Post subject:  RE:Wall Help

Figure out where the wall is.

On paper. With math.

You already have the numbers -- you are using them to draw the wall in the first place. Although it might be easier to exploit arrays (think tiles) and write a general solution once than having to hard-code every block location (that gets messy).

Posted Image, might have been reduced in size. Click Image to view fullscreen.
(not really a wall, but it's semi-relevant)

Author:  Insectoid [ Fri Sep 05, 2008 12:46 pm ]
Post subject:  RE:Wall Help

Sorry, tony, I know you love paper and math, but I can't help but give a link of my own.

Tutorial!

Author:  S_Grimm [ Mon Sep 08, 2008 10:36 am ]
Post subject:  RE:Wall Help

if y location of box is greater than y location of top wall, then y := y -1

Author:  Warchamp7 [ Mon Sep 08, 2008 11:02 am ]
Post subject:  Re: RE:Wall Help

A\V @ Mon Sep 08, 2008 10:36 am wrote:
if y location of box is greater than y location of top wall, then y := y -1


1. He's talking about blocks not a roof

2. That wouldn't work if he moves more than 1 pixel every loop which is likely unless that loop is 1


Collisions in turing are not a simple task if you're still getting your bearings. You'll need to have collision checks between the object you control and anything that can restrict it's movement. Now if you only had two objects it's not too difficult but with more it becomes a little more daunting.

Unless you know how to use arrays effectively it won't be fun tracking multiple collisions : /

Author:  Boiq [ Mon Sep 08, 2008 11:06 am ]
Post subject:  RE:Wall Help

Okay, i'm starting to understand a bit.

And yes i'm talking about a collosion with the walls. Isn't it kinda binary, like 0's for path, 1's for walls, 2 for your character, etc?

Author:  Warchamp7 [ Mon Sep 08, 2008 11:07 am ]
Post subject:  RE:Wall Help

If you used a tile system yes

Author:  S_Grimm [ Mon Sep 08, 2008 11:43 am ]
Post subject:  Re: Wall Help

This is whant you want to do right?
sorry asbout the rough code. I need to fix the top and right boundries


code:

var x, y : int := 30 %starting position of box
var x1, y1 : int := 40
var move := 5 %change this variable to change movement
var key : array char of boolean
procedure bound
    if (x >= 300) then
        x := x - move
        x1 := x1 - move
    end if
    if x <= 20 then
        x := x + move
        x1 := x1 + move
    end if
    if (y >= 300) then
        y := y - move
        y1 := y1 - move
    end if
    if y <= 20 then
        y := y + move
        y1 := y1 + move
    end if
end bound
procedure movement
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        y := y + move
        y1 := y1 + move
    end if
    if key (KEY_RIGHT_ARROW) then
        x := x + move
        x1 := x1 + move
    end if
    if key (KEY_LEFT_ARROW) then
        x := x - move
        x1 := x1 - move
    end if
    if key (KEY_DOWN_ARROW) then
        y := y - move
        y1 := y1 - move
    end if
end movement

View.Set ("offscreenonly")
loop
Draw.FillBox (20, 20, 300, 300, black)
    Draw.FillBox (x, y, x1, y1, red)
    bound
    movement   
    View.Update
end loop

Author:  Warchamp7 [ Mon Sep 08, 2008 11:57 am ]
Post subject:  RE:Wall Help

If he's doing a top down then that seems right, though once again you're just doing outer boundaries.

I think he's looking for collisions with a block.

So for example using your rough code
Posted Image, might have been reduced in size. Click Image to view fullscreen.

Creating collisions with the white areas

Author:  Insectoid [ Mon Sep 08, 2008 3:34 pm ]
Post subject:  RE:Wall Help

Outer boundaries? That's what you wanted? That's easy.

In pseudocode,

if the right button is pressed and the X value is less then maxx, incease X by 1.

Author:  S_Grimm [ Tue Sep 09, 2008 6:41 pm ]
Post subject:  RE:Wall Help

ok so you want the code to look like :
if x location of figure = right edge of box, then x := x - movement pixels
end if
for a specific size of box where you need to go over it also:
if x location of figure is = right edge of box and y location of figure is >= bottom of box and y location <= top of box then
x := x - movement pixels
end if

Author:  Warchamp7 [ Wed Sep 10, 2008 7:43 am ]
Post subject:  Re: RE:Wall Help

A\V @ Tue Sep 09, 2008 6:41 pm wrote:
ok so you want the code to look like :
if x location of figure = right edge of box, then x := x - movement pixels
end if
for a specific size of box where you need to go over it also:
if x location of figure is = right edge of box and y location of figure is >= bottom of box and y location <= top of box then
x := x - movement pixels

end if


If I read that right, you just made the character only move if it's INSIDE the box touching the right side of it.

Author:  S_Grimm [ Wed Sep 10, 2008 9:37 am ]
Post subject:  Re: Wall Help

Warchamp, Here is an example of the conde. you cannot move the red box through the side of the white box. (you can still move through the top and bottom though. i wrote this really fast.

Turing:

setscreen ("offscreenonly")
var x1, y1 : int := 300
var x2, y2 : int := 320
var key : array char of boolean
var movement : int := 1
procedure bound
if x1 +20 = 50 and y2 -20 <= 200 and y2 >= 50 then %account for the box's size, offset the variable by size of the box
x1 := x1 - movement
x2 := x2 - movement
end if
if x1 = 150 and y1 <= 200 and y1 >= 50 then
x1 := x1 + movement
x2 := x2 + movement
end if
end bound

loop
View.Update
Draw.FillBox (0, 0, maxx, maxy, black)
Draw.FillBox (50, 50, 150, 200, white)
Draw.FillBox (x1,y1,x2,y2, red)
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        y1 := y1 + movement
        y2 := y2 + movement
    end if
    if key (KEY_RIGHT_ARROW) then
        x1 := x1 + movement
        x2 := x2 + movement
    end if
    if key (KEY_LEFT_ARROW) then
        x1 := x1 - movement
        x2 := x2 - movement
    end if
    if key (KEY_DOWN_ARROW) then
        y1 := y1 - movement
        y2 := y2 - movement
    end if
bound
end loop

Author:  Warchamp7 [ Wed Sep 10, 2008 11:38 am ]
Post subject:  Re: Wall Help

Quote:
if x location of figure is = right edge of box and y location of figure is >= bottom of box and y location <= top of box then
x := x - movement pixels


code:
if x1 + 20 = 50 and y2 -20 <= 200 and y2 >= 50 then


As I pretty much said, this only works if he is perfectly at 50.

What happens if the speed is 4? It's good to make an area to check for collisions. A literal hitbox.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Author:  isaiahk9 [ Thu Sep 11, 2008 12:00 pm ]
Post subject:  RE:Wall Help

One of the easiest ways I've found is just like this :

if the user clicks "move right" and characterx < x coordinate of the right side of the wall and character x > x coordinate of the left side then

the character can move any direction

else

the character can only move left.

Kind of confusing the way I wrote it, but basically your character cannot go in the area that is designated wall.

Author:  Insectoid [ Thu Sep 11, 2008 12:06 pm ]
Post subject:  RE:Wall Help

Warchamp, there is a simple solution.

code:

if playerx >= maxx then
    playerx := maxx
end if


Of course, this would be incorporated into the movement. If the player skips over maxx by moving multiple pixel at a time, he gets teleported back to the inside. It sounds buggy, but it works so fast you don't notice the flashing.

Author:  isaiahk9 [ Thu Sep 11, 2008 3:54 pm ]
Post subject:  RE:Wall Help

That's sorta what I meant, Insectoid, except with walls in the middle of the screen instead of on the limits.

Author:  Warchamp7 [ Thu Sep 11, 2008 9:00 pm ]
Post subject:  Re: RE:Wall Help

insectoid @ Thu Sep 11, 2008 12:06 pm wrote:
Warchamp, there is a simple solution.

code:

if playerx >= maxx then
    playerx := maxx
end if


Of course, this would be incorporated into the movement. If the player skips over maxx by moving multiple pixel at a time, he gets teleported back to the inside. It sounds buggy, but it works so fast you don't notice the flashing.


You all fail to see what I'm pointing out

You're referring to the edge of the game field

I'm referring to the edge of the "block"

The code he posted ONLY restricts movement right if the playerx is EXACTLY 50 or against the block. Now, if speed is 4, the playerx goes from 48 to 52 and as such, the check fails.

The best choice would be checking an AREA, not just one pixels width worth

Author:  isaiahk9 [ Fri Sep 12, 2008 5:05 am ]
Post subject:  RE:Wall Help

if player presses right and playerx >= rightsideofbox and playerx <= leftsideofbox and playery <= topofthebox then
playerx -= 10
elsif player presses left playerx >= rightsideofbox and playerx <= leftsideofbox and playery <= topofthebox then
playerx += 10
elsif playerx >= rightsideofbox and playerx <= leftsideofbox then
playery = height of box += 10
else
%code for other stuff
end if

Author:  S_Grimm [ Tue Sep 16, 2008 7:46 am ]
Post subject:  RE:Wall Help

the cod i posted said if playerx >= 50 not =50.

od all you have to do is change the code so that your speed is a set variable then
if playerx >= side of box then
playerx := playerx - speed
end if

that way it would always return you to your previous starting position.
edit:
here is the code as you want it warchamp. with the movement at 4

Turing:


setscreen ("offscreenonly")
var x1, y1 : int := 300
var x2, y2 : int := 320
var key : array char of boolean
var movement : int := 4
procedure bound
if x1 +20 >= 50 and x1 +20 <=150 and y2 -20 <= 200 and y2 >= 50 then %account for the box's size, offset the variable by size of the box
x1 := x1 - movement
x2 := x2 - movement
end if
if x1 <= 150 and x1 >= 50 and y1 <= 200 and y1 >= 50 then
x1 := x1 + movement
x2 := x2 + movement
end if
end bound

loop
View.Update
Draw.FillBox (0, 0, maxx, maxy, black)
Draw.FillBox (50, 50, 150, 200, white)
Draw.FillBox (x1,y1,x2,y2, red)
    Input.KeyDown (key)
    if key (KEY_UP_ARROW) then
        y1 := y1 + movement
        y2 := y2 + movement
    end if
    if key (KEY_RIGHT_ARROW) then
        x1 := x1 + movement
        x2 := x2 + movement
    end if
    if key (KEY_LEFT_ARROW) then
        x1 := x1 - movement
        x2 := x2 - movement
    end if
    if key (KEY_DOWN_ARROW) then
        y1 := y1 - movement
        y2 := y2 - movement
    end if
bound
end loop

Author:  Warchamp7 [ Wed Sep 17, 2008 8:40 am ]
Post subject:  Re: RE:Wall Help

A\V @ Tue Sep 16, 2008 7:46 am wrote:
the cod i posted said if playerx >= 50 not =50.

Newest Code
Turing wrote:

if x1 +20 >= 50 and x1 +20 <=150 and y2 -20 <= 200 and y2 >= 50 then %account for the box's size, offset the variable by size of the box


Old Code
Turing wrote:

if x1 +20 = 50 and y2 -20 <= 200 and y2 >= 50 then


Mr. Green

Author:  S_Grimm [ Thu Sep 18, 2008 7:16 am ]
Post subject:  RE:Wall Help

ok. sorry. i guess i forgot the > sign. but the new code does exactly what you want it to, right warchamp? the movement can be changed to different increments and you still are refused entry to the side of the box.

Author:  Warchamp7 [ Thu Sep 18, 2008 7:58 am ]
Post subject:  Re: RE:Wall Help

A\V @ Thu Sep 18, 2008 7:16 am wrote:
ok. sorry. i guess i forgot the > sign. but the new code does exactly what you want it to, right warchamp? the movement can be changed to different increments and you still are refused entry to the side of the box.


Yes, it creates a hitbox which are used in many games today.

It's good to get used to them IMO

Author:  S_Grimm [ Thu Sep 18, 2008 12:41 pm ]
Post subject:  RE:Wall Help

I believe that that was the point of the original topic. if not, please post something to say otherwise, boiq

Author:  Insectoid [ Thu Sep 18, 2008 12:54 pm ]
Post subject:  RE:Wall Help

Warchamp, once again, there is a simple solution. My code does NOT only check one pixel's worth. It checks to see if the character has gone over the line, and if he has, he gets teleported back to the edge of the screen. It checks for EVERY PIXEL beyond the limits of where the character is supposed to go. He could move 50 pixels at a time and it would work.

Author:  S_Grimm [ Fri Sep 19, 2008 8:27 am ]
Post subject:  RE:Wall Help

he wanted the box in the middle of the screen NOT the edge. Please read the previous posts before you respond. I repeat IN THE MIDDLE OF THE SCREEN MOT THE EDGE.


: