Character fails to interact properly with two faces of a wall
Author |
Message |
Legion
|
Posted: Sun Mar 23, 2014 3:49 pm Post subject: Character fails to interact properly with two faces of a wall |
|
|
What is it you are trying to achieve?
I want the character to be unable to pass through a square.
What is the problem you are having?
My character only behaves the way he is supposed to for 2 sides of the wall.
Describe what you have tried to solve this problem
I do not know what I am doing wrong.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
var x, y : int
x := 0
y := 0
var chars : array char of boolean
var box1_x1, box1_y1, box1_x2, box1_y2 : int
box1_x1 := 100
box1_y1 := 100
box1_x2 := 150
box1_y2 := 150
loop
drawbox (box1_x1, box1_y1, box1_x2, box1_y2, black)
Input.KeyDown (chars )
if chars ('w') then
y := y + 5
end if
if chars ('d') then
x := x + 5
end if
if chars ('a') then
x := x - 5
end if
if chars ('s') then
y := y - 5
end if
drawfillbox (x, y, x + 5, y + 5, black)
if x = box1_x1 and y > box1_y1 and y < box1_y2 then
x: = x - 5
end if
if x = box1_x2 and y > box1_y1 and y < box1_y2 then
x: = x + 5
end if
if y = box1_y2 and x > box1_x1 and x < box1_x2 then
y: = y + 5
end if
if y = box1_y1 and x > box1_x1 and x < box1_x2 then
y: = y - 5
end if
delay (70)
cls
end loop
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Sun Mar 23, 2014 5:00 pm Post subject: RE:Character fails to interact properly with two faces of a wall |
|
|
Your character is a 5x5 square. In your collision detection you are treating it as a point at (x, y). What does the screen look like when y = box1_y1? Draw it on paper if you have to in order to see it. |
|
|
|
|
![](images/spacer.gif) |
Legion
|
Posted: Sun Mar 23, 2014 5:47 pm Post subject: RE:Character fails to interact properly with two faces of a wall |
|
|
Hey Insectoid, thanks for the reply!
Anyway, correct me if I am wrong but wouldn't that just be a horizontal line at box1_y1 running infinitely? If so, I do not really see what you are trying to point out. Also, how do you think I can change the collision detection to treat the interaction as one between the two entities rather than as points. Or perhaps there is another easier way? |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sun Mar 23, 2014 6:07 pm Post subject: Re: Character fails to interact properly with two faces of a wall |
|
|
Legion wrote:
Hey Insectoid, thanks for the reply!
Anyway, correct me if I am wrong but wouldn't that just be a horizontal line at box1_y1 running infinitely? If so, I do not really see what you are trying to point out. Also, how do you think I can change the collision detection to treat the interaction as one between the two entities rather than as points. Or perhaps there is another easier way?
He means what does your program look like when the variable y is equal to box1_y1. |
|
|
|
|
![](images/spacer.gif) |
Legion
|
Posted: Sun Mar 23, 2014 6:20 pm Post subject: RE:Character fails to interact properly with two faces of a wall |
|
|
I do sincerely apologize for my incompetence.
Nonetheless, if y is equal to box1_y1 then am I not just changing the starting location of the character, as that is what I had created the variable to do; store the coordinates of the character.
Again, I really do apologize for not following what the two of you are trying to say, as I know what I just said is wrong but I don't know what else to think.
I should, however, thank the two of you for attempting to help me. |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sun Mar 23, 2014 6:24 pm Post subject: Re: Character fails to interact properly with two faces of a wall |
|
|
The point is that the variables x and y represent the lower left corner of your character, so when y = box1_y1 your character is drawn with its lower left corner outside the box (which doesn't guarantee that your entire character is outside the box). |
|
|
|
|
![](images/spacer.gif) |
Legion
|
Posted: Sun Mar 23, 2014 7:17 pm Post subject: Re: Character fails to interact properly with two faces of a wall |
|
|
But of course!
That explains why the bottom wall and left wall did not function properly!
I think that the fixed code will be this:
code: |
%Variables are declared
var x1, y1, x2, y2 : int
x1 := 0
y1 := 0
x2 := x1 + 5
y2 := y1 + 5
var chars : array char of boolean
var box1_x1, box1_y1, box1_x2, box1_y2 : int
box1_x1 := 100
box1_y1 := 100
box1_x2 := 150
box1_y2 := 150
%Movement is initialized and checks for obstacles are set in place
loop
drawbox (box1_x1, box1_y1, box1_x2, box1_y2, black)
Input.KeyDown (chars)
if chars ('w') then
y1 := y1 + 5
y2 := y2 + 5
end if
if chars ('d') then
x1 := x1 + 5
x2 := x2 + 5
end if
if chars ('a') then
x1 := x1 - 5
x2 := x2 - 5
end if
if chars ('s') then
y1 := y1 - 5
y2 := y2 - 5
end if
drawfillbox (x1, y1, x2, y2, black)
%The obstacle's perimeter is defined
if x1 = box1_x1 and y1 > box1_y1 and y1 < box1_y2 then
x1 := x1 - 5
x2 := x2 - 5
end if
if x1 = box1_x2 and y1 > box1_y1 and y1 < box1_y2 then
x1 := x1 + 5
x2 := x2 + 5
end if
if y1 = box1_y2 and x1 > box1_x1 and x1 < box1_x2 then
y1 := y1 + 5
y2 := y2 + 5
end if
if y1 = box1_y1 and x1 > box1_x1 and x1 < box1_x2 then
y1 := y1 - 5
y2 := y2 - 5
end if
if x2 = box1_x1 and y2 > box1_y1 and y2 < box1_y2 then
x2 := x2 - 5
x1 := x1 - 5
end if
if x2 = box1_x2 and y2 > box1_y1 and y2 < box1_y2 then
x2 := x2 + 5
x1 := x1 + 5
end if
if y2 = box1_y2 and x2 > box1_x1 and x2 < box1_x2 then
y2 := y2 + 5
y1 := y1 + 5
end if
if y2 = box1_y1 and x2 > box1_x1 and x2 < box1_x2 then
y2 := y2 - 5
y1 := y1 - 5
end if
%A delay is set so that the objects can be seen before disappearing
delay (50)
cls
end loop
|
Sorry for not using the syntax code, but for some reason it was not working.
The top right and bottom left corners don't work properly this way, but I doubt that my teacher will notice.
Anyway, thank you so much Dreadnought! |
|
|
|
|
![](images/spacer.gif) |
Dreadnought
|
Posted: Sun Mar 23, 2014 7:51 pm Post subject: Re: Character fails to interact properly with two faces of a wall |
|
|
Here's a link to a post on collision detection in Turing that might interest you
http://compsci.ca/v3/viewtopic.php?t=13661 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|