Help with Collision detection.
Author |
Message |
recneps
|
Posted: Thu Feb 26, 2004 9:19 pm Post subject: Help with Collision detection. |
|
|
I am making a game, and im gonna have blocks, which if you try to go inside one, collision is true, and if its true then you cant move in. Got that? well heres my code, there something wrong. if you go "too far" in a wall, you get stuck. ive been over code like 50 times. Can anyone find the problem?
code: |
setscreen ("graphics:300;300;nobuttonbar;Title:Game")
setscreen ("offscreenonly")
var chars : array char of boolean
var x, y : int := 150
const left : char := KEY_LEFT_ARROW
const right : char := KEY_RIGHT_ARROW
const up : char := KEY_UP_ARROW
const down : char := KEY_DOWN_ARROW
const numblocks : int := 120
var xb, yb : array 0 .. numblocks of int
var collision : boolean := false
var num : int := 0
const charwidth : int := 5
var movex, movey : int := 0
procedure drawblock (x : int, y : int)
drawfillbox (x, y, x + 10, y + 10, blue)
end drawblock
procedure drawchar (x : int, y : int)
drawfilloval (x, y, charwidth, charwidth, red)
end drawchar
procedure checkcollision (x : int, y : int)
for i : 1 .. numblocks
if x <= xb (i) + 10 and x >= xb (i) and y <= yb (i) + 10 and y >= yb (i) then
collision := true
end if
end for
end checkcollision
procedure drawborder
num := 0
for a : 0 .. 30
xb (a) := 0
yb (a) := num * 10 %left wall coords
num += 1
end for
num := 0
for a : 31 .. 60
xb (a) := num * 10
yb (a) := 290 %draw top wall
num += 1
end for
num := 0
for a : 61 .. 90
xb (a) := 290
yb (a) := num * 10 %draw right wall
num += 1
end for
num := 0
for a : 91 .. 120
xb (a) := num * 10
yb (a) := 0 %draw bottom wall
num += 1
end for
for i : 0 .. numblocks
drawblock (xb (i), yb (i))
end for
end drawborder
var oldx, oldy := 0
loop
oldx := x
oldy := y
Input.KeyDown (chars)
cls
drawborder
drawchar (x, y)
if chars (up) then
y += 5
end if
if chars (down) then
y -= 5
end if
if chars (left) then
x -= 5
end if
if chars (right) then
x += 5
end if
if chars (chr (27)) then
return
end if
checkcollision (x, y)
if collision = true then
x := oldx
y := oldy
end if
View.Update
delay (25)
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Paul
|
Posted: Thu Feb 26, 2004 9:29 pm Post subject: (No subject) |
|
|
I dunno, cause I can't figure out your code without looking at it closely, and I don't wanna do that, cause Im too tired, but I made the same kind of program, the only difference is using whatdotcolor instead of the coordinates. |
|
|
|
|
|
Tony
|
Posted: Thu Feb 26, 2004 11:45 pm Post subject: (No subject) |
|
|
i dont see collision := false anywhere in your program... once it is made to be true, it will always stay that way
instead you should make collisions into a function that returns true/false... such as
code: |
function collide(x,y:int) :boolean
for i:1..num
if x<=xb(i)+10 and...
result true
end if
end for
result false %if forloop has finished without returning true, it must be false
end collide
|
and in the program you just use the function in your if statement
code: |
if collide(myX, myY) then
... |
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
recneps
|
Posted: Fri Feb 27, 2004 4:40 pm Post subject: (No subject) |
|
|
Haha, im so stupid. Thanks tony ill get on fixing that. I knew it was a simple problem. |
|
|
|
|
|
|
|