Author Topic: [Solved] Collision Detection: what's wrong with this code?  (Read 2145 times)

0 Members and 1 Guest are viewing this topic.

Offline Darl181

  • «Yo buddy, you still alive?»
  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3408
  • Rating: +305/-13
  • VGhlIEdhbWU=
    • View Profile
    • darl181.webuda.com
[Solved] Collision Detection: what's wrong with this code?
« on: June 28, 2011, 03:49:42 am »
Hi, I've been debugging collision detection equations like these for the past few hours, and every "fix" I'd try would only make it worse.  So I figured it's about time to post it.

Basically what it's supposed to do is have a square, controllable via arrow keys, that moves amongst other squares while not going into them.  Think TWHG with just the player and the walls, and 4*4, and you'll have about the right idea.

Problem is, something's really screwed up in the way it looks for other blocks, and seems to be taking data from who knows where in the ram...

When you press alpha, it displays what the program thinks is under each of the corners laid out like this
I  J
N  O

0 is empty, 1 is occupied.

Also if you hold [2nd] when you press an arrow key it displays what the program thinks is just above/below/whatever in relation to the player sprite.

Code: [Select]
:.ATST3
:[F09090F000000000]?Pic1
:[F0F0F0F000000000]?Pic2
:48?D
:For(T,2,15)
:For(S,0,23)
:If rand^2
:1?{D+L1}
:Pt-Change(S*4,T*4,Pic2)
:End
:D++
:End
:End
:4?X?Y
:Repeat getKey(15)
:Y/4*24+(X/4)+L1?I
:Y/4*24+(X+3/4)+L1?J
:Y+3/4*24+(X/4)+L1?N
:Y+3/4*24+(X+3/4)+L1?O
:If getKey(48)
:ClrHome
:Disp "I",{I}?Dec,i,"J",{J}?Dec,i,"N",{N}?Dec,i,"O",{O}?Dec,i,I?Dec,J?Dec,i,N?Dec,O?Dec
:.those are the newline "i"s btw
:While getKey(48)
:Pause 10
:End
:End
:If getKey(1)
:If getKey(54)
:ClrHome
:Disp {I+24}?Dec,{J+24}?Dec
:While getKey(54)
:Pause 10
:End
:End
:If {I+24} or {J+24}
:Else
:Y++
:End
:End
:
:If getKey(4)
:If getKey(54)
:ClrHome
:Disp {N-24}?Dec,{O-24}?Dec
:While getKey(54)
:Pause 10
:End
:End
:If {N-24} or {O-24}
:Else
:Y--
:End
:End
:
:If getKey(2)
:If getKey(54)
:ClrHome
:Disp {J-1}?Dec,{O-1}?Dec
:While getKey(54)
:Pause 10
:End
:End
:If {J-1} or {O-1}
:Else
:X--
:End
:End
:
:If getKey(3)
:If getKey(54)
:ClrHome
:Disp {I+1}?Dec,{N+1}?Dec
:While getKey(54)
:Pause 10
:End
:End
:If {I+1} or {N+1}
:Else
:X++
:End
:End
:Pt-Change(X,Y,Pic1)
:DispGraph
:Pt-Change(X,Y,Pic1)
:End

Needless to say this isn't my contest entry, but it's somewhat related.  One minor bugfix led to another and it spiraled into a revamped system for detection, which this is an oversimplification/unoptimization of.
« Last Edit: June 28, 2011, 04:14:49 pm by Darl181 »
Vy'o'us pleorsdti thl'e gjaemue

Offline Darl181

  • «Yo buddy, you still alive?»
  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3408
  • Rating: +305/-13
  • VGhlIEdhbWU=
    • View Profile
    • darl181.webuda.com
Re: Collision Detection: what's wrong with this code?
« Reply #1 on: June 28, 2011, 11:32:40 am »
Bump.
Still not getting it to work properly :(


EDIT: wait, parentheses needed..
EDIT2: put in parenthesis and it still works just the same..meaning it's not working

« Last Edit: June 28, 2011, 04:14:00 pm by Darl181 »
Vy'o'us pleorsdti thl'e gjaemue

Offline Darl181

  • «Yo buddy, you still alive?»
  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3408
  • Rating: +305/-13
  • VGhlIEdhbWU=
    • View Profile
    • darl181.webuda.com
Re: Collision Detection: what's wrong with this code?
« Reply #2 on: June 28, 2011, 04:14:33 pm »
Got it to work.  Turns out it just wasn't initialized properly so there was still a bunch of random stuff in L1 :P

I also got a simple game idea from this...more on that later ;)
Vy'o'us pleorsdti thl'e gjaemue