Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: meishe91 on March 09, 2010, 05:52:17 pm

Title: Collision Detection Help
Post by: meishe91 on March 09, 2010, 05:52:17 pm
So I have been planning on making a homescreen maze type game (there is still a lot I need to figure out haha). The problem I'm having though is that I was trying to make a test program that would just give me the way to move the character. For some reason though it isn't working and I can't figure out what the problem is 

Code: [Select]
PROGRAM:MOVE
"//////////////// //Broke it down so it's easier to see.
/___///___///__/
/_///___///__/_/
/______/////___/
/__///__///__/_/
/_////__//__////
/______________/
////////////////→Str1
Output(1,1,Str1
2→A
2→B
Repeat 0
Output(A,B,"*
Repeat Ans
getKey→K
End
Output(A,B,"_
A+(Ans=34)(sub(Str1,AB+16,1)="_")-(Ans=25)(sub(Str1,AB-16,1)="_→A
B+(K=26)-(K=24)→B
End

I know it is happening in the sub( parts. Which I'm guessing they are probably wrong, they just look correct to me.
Thanks for any help.

By the way, I'm aware this is not the most efficient method of doing this. Just trying to do this without looking up help code from something like TIBD. O0
Title: Re: Collision Detection Help
Post by: ztrumpet on March 09, 2010, 06:02:37 pm
Think this would work:
Code: [Select]
PROGRAM:MOVE
"//////////////// //Broke it down so it's easier to see.
/___///___///__/
/_///___///__/_/
/______/////___/
/__///__///__/_/
/_////__//__////
/______________/
////////////////→Str1
Output(1,1,Ans //Save speed and a byte :)
2→A
2→B
Repeat 0
Output(A,B,"*
Repeat Ans
getKey→K
End
Output(A,B,"_
A+(Ans=34 and sub(Str1+"________________",16A+B,1)="_")-(Ans=25 and sub("________________"+Str1,16A+B-16,1)="_→A //That is 16 spaces both times
B+(K=26 and sub(Str1+"_",16A+B-15,1)="_")-(K=24 and sub("_"+Str1,16A+B-16,1)="_→B //One space each time
End
Good luck! ;D

Edit: Did I just double ninja?!?!  Wow!  And I beat Nyrax by 3 seconds! ;D
Title: Re: Collision Detection Help
Post by: Radical Pi on March 09, 2010, 06:02:40 pm
Disregarding the fact that you could totally rewrite that code for efficiency...
Change AB+16 to 16A+B, and change AB-16 to 16A-32+B.
Warning, though, this solution still doesn't take into account when you're trying to move off the screen, so do something about that. I just fixed your subs :P

EDIT: Argh, beaten with a more in depth solution
Title: Re: Collision Detection Help
Post by: tifreak on March 09, 2010, 06:04:38 pm
To be truthful, I never was able to get a proper hit detection working like this. Mainly because not only did I have the 2 variables to move the character around, I had a third that was used to test the string as it moved. It eliminated a great many problems I had with hit detection and still had a pretty decent speed.

I have an RPG Starter Kit that uses a similar method that you are trying to make, but won't show unless asked for.

And I am pretty sure your A+(Ans=34)(sub(Str1,AB+16,1)="_")-(Ans=25)(sub(Str1,AB-16,1)="_→A won't work.

say you are on the 3rd row, 2nd colum. A*B, or in this case, 3*2=6. So the string is looking at the 6th space. it doesn't work. =/

I am sure if enough thought is put into it, something could be made, but really, you will save yourself a ton of headaches if you have a third variable that keeps track of where the character is at in the string.
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 06:06:49 pm
Thanks! Ztrumpet, could you maybe explain it a little? Or anyone haha. I see where it went wrong but not sure how it is being fixed (especially with the adding of spaces).
Title: Re: Collision Detection Help
Post by: Radical Pi on March 09, 2010, 06:10:21 pm
tifreak: But your current location in the string is just 16A-16+B at any given time, assuming you're using the typical 8x16 homescreen map string. That's not really worth its own variable in my opinion.

meishe91: Your location in the string at any time is just 16A-16+B. To get the above space, subtract 16 from that. To get the left space, subtract 1. Down: +16. Right: +1. See how it works?
Title: Re: Collision Detection Help
Post by: tifreak on March 09, 2010, 06:13:33 pm
I found it to be easier when dealing with strings, but this was 5 years ago when I first developed the code for my engine that I use. At the time I hadn't thought of it that way, and currently suffering from a lack of sleep, so that doesn't really help. Besides, I use 4 way scrolling with a stationary character, my variable for the character location is mandatory now. :p
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 06:13:52 pm
Thank you guys.
I see what you mean tifreak, it made sense in my head earlier but it didn't click. Hate when things like that happen. That third value method sounds interesting though, I might try that. I'm just trying to get a basic thing working first though so I can move from there :)

Ohhhhhhhhhhhhh, I see. Ok, ya that makes sense. I see what I was doing wrong when working the math out. Hate when simple things get past ya. But in ztrumpet's, I don't see what adding the spaces does. (Thanks though, Nyrax.)
Title: Re: Collision Detection Help
Post by: Radical Pi on March 09, 2010, 06:18:12 pm
What spaces? *looks*
Ah, ingenious! Those spaces prevent domain errors from when you're trying to go up at the top of the screen, or down at the bottom of the screen, or the same for the sides (although those should only matter in the very first and very last place in the string). I never thought to do it that way!

EDIT: 8 seconds. We're even now :P *ZTrumpet lols...
Title: Re: Collision Detection Help
Post by: ztrumpet on March 09, 2010, 06:18:20 pm
But in ztrumpet's, I don't see what adding the spaces does. (Thanks though, Nyrax.)
The extra spaces let you go off the screen without it erroring. :)
Try it like this if you would like to see:
Code: [Select]
PROGRAM:MOVE
"//////////////// //Broke it down so it's easier to see.
/___///___///__/
/_///___///__/_/
/______/////___/
/__///__///__/_/
/_////__//__////
/______________/
////////////////→Str1
Output(1,1,Ans //Save speed and a byte :)
2→A
2→B
Repeat 0
Output(A,B,"*
Repeat Ans
getKey→K
End
Output(A,B,"_
A+(Ans=34 and sub(Str1,16A+B,1)="_")-(Ans=25 and sub(Str1,16A+B-16,1)="_→A
B+(K=26 and sub(Str1,16A+B-15,1)="_")-(K=24 and sub(Str1,16A+B-17,1)="_→B
End

Edit: My method won't work. :(  It will still sometimes fail when you go off the screen.  Let me think of a new solution... :)
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 06:27:16 pm
Oh, ok, I was just about to say it doesn't quite...this is really starting to bug me though because it seems like it shouldn't be this hard :P

BOO YA! I got it to work!!!!! I'll post it in a second.
Title: Re: Collision Detection Help
Post by: Radical Pi on March 09, 2010, 06:33:30 pm
This is just me brainstorming without testing my thoughts with code, but I think ztrumpet's code fails because Str1 doesn't get updated to include the nether zone that you can move into. But if you did add all that whitespace to Str1 every time you went farther out of bounds, it would just open up a whole new can of worms regarding tracking your location in the string. And fixing all those new problems would just make this a horribly inefficient piece of code I would think, when so many alternatives are possible.

Maybe you should either completely disallow going offscreen, or make the Output(A,B,"* line be conditionally dependent on being onscreen.
Title: Re: Collision Detection Help
Post by: ztrumpet on March 09, 2010, 06:39:24 pm
Here's my completely different code that I am 100% sure it works. ;D

Code: [Select]
"String->Str1  //Note: Put the real string here, not "String". =D
Output(1,1,Ans
2->A
2->B
Output(Ans,A,"*
DelVar C0
Repeat A+C=17 or B+Ans=9 or not((A+C)(B+Ans
If "_"=sub(Str1,16(B+Ans)+A+C-16,1
Then
Output(B,A,"_
B+Ans->B
A+C->A
Output(B,A,"*
End
Repeat Ans
getKey->K
End
(Ans=26)-(Ans=24->C
(K=34)-(K=25
End
Good luck! ;D
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 06:39:32 pm
Ok, so what was happening is that the position of the character was being determined wrong. Instead of 16A+B it is actually  16A-(16-B).

So, the code would be:

Code: [Select]
PROGRAM:MOVE
"////////////////
/___///___///__/
/_///___///__/_/
/______/////___/
/__///__///__/_/
/_////__//__////
/______________/
////////////////→Str1
Output(1,1,Ans
2→A
2→B
Repeat 0
Output(A,B,"*
Repeat Ans
getKey→K
End
Output(A,B,"_
A+(Ans=34 and sub(Str1,16A-(16-B)+16,1)="_")-(Ans=25 and sub(Str1,16A-(16-B)-16,1)="_→A
B+(K=26 and sub(Str1,16A-(16-B)+1,1)="_")-(K=24 and sub(Str1,16A-(16-B)-1,1)="_→B
End
Title: Re: Collision Detection Help
Post by: ztrumpet on March 09, 2010, 06:41:14 pm
Wow, this is a really hot topic.  Did you try my solution? :D
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 06:42:02 pm
Not quite yet. I'm kinda still excited that I figured it out :D I will in a second.

Edit: I tried it out. Nice, not sure I follow it all (just 'cause I haven't looked at it extensively) but cool. Thanks :)

The only thing I'm not following is why are you implementing the boundaries in this? The way they are working shouldn't as long as you have the outer boarder (like my string has) it shouldn't go off screen, right? (I know that is how my program works anyways.)
Title: Re: Collision Detection Help
Post by: Radical Pi on March 09, 2010, 06:53:14 pm
Your most recent code doesn't work if you ever go into any of the screen's edges (A=1 or A=8 or B=1 or B=16) because even if you might not be trying to move in the offscreen direction, your code still ends up testing each space adjacent to you, of which one will be offscreen and cause an error.

It would be much easier to fix this up if we knew whether or not your maze would ever allow you onto those edges :P

EDIT: And there's my answer. So the problem is solved totally now?
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 06:59:17 pm
Well ya, I know that. But for testing purposes this string has boarders all around the edge. I know once I get to work on the maze I will need to implement going off the edge. Thanks though :)
Title: Re: Collision Detection Help
Post by: ztrumpet on March 09, 2010, 07:24:17 pm
Ah, I see.  I was working on making it go off the screen. :)
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 07:26:51 pm
Ya, I will have to implement that into the maze game. Still have a lot to figure out :P
Title: Re: Collision Detection Help
Post by: DJ Omnimaga on March 09, 2010, 08:11:41 pm
I always had massive trouble with detecting collision when going out of the screen. In the past, I would make my matrices 10x18 instead of 8x16 but that wastes a lot of RAM
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:08:22 pm
Hmmm, interesting.
Title: Re: Collision Detection Help
Post by: Builderboy on March 09, 2010, 11:19:06 pm
I always had massive trouble with detecting collision when going out of the screen. In the past, I would make my matrices 10x18 instead of 8x16 but that wastes a lot of RAM

Thats exactly what i did for Shift :) Maybe it wastes a but of mem, but it sure makes the engine a hell of a lot faster ;D I guess it depends on what kind of game your writing.  I had non tile-by-tile movement so I needed all the speed i could get O.O
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:24:31 pm
I'm not entirely sure I understand what increasing the matrix by 2 in both ways does. Could someone explain that please?
Title: Re: Collision Detection Help
Post by: Builderboy on March 09, 2010, 11:29:18 pm
Basically you have a border around your entire map that is not displayed, but is there so that when you try to move off the screen, the collision routine has something to find (or else it goes wacky).  This can be avoided by clever coding, but often times the code needs to be very very clever to still be fast.
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:31:43 pm
Oh, ok. I get it. Interesting. Then why two more layers? Wouldn't a 9x17 work just as well?
Title: Re: Collision Detection Help
Post by: Builderboy on March 09, 2010, 11:34:15 pm
well you need to increase it on both sides ;)
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:37:19 pm
Oh! Wow, I'm tired. (Note to self: Don't let yourself get less than 30 min of sleep again...). Gotcha :D
Title: Re: Collision Detection Help
Post by: Builderboy on March 09, 2010, 11:40:38 pm
Heh i feel your pain, getting up at 5:00 every day for band is killing me X.x
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:41:49 pm
You get up at 5:00 for band?
Title: Re: Collision Detection Help
Post by: Builderboy on March 09, 2010, 11:46:18 pm
Yeahh... :( And what sucks is that when we all signed up, we were only supposed to have practices at night on wendsdays and fridays.  But now its every day at this ridiculous time >:( He even wanted one girl to reschedual her SAT's so she could make it to a competition.  She refused.
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:47:56 pm
Is it for marching?
Title: Re: Collision Detection Help
Post by: Builderboy on March 09, 2010, 11:50:31 pm
Yeah we do winter Percussion, but this is getting slightly offtopic now :P
Title: Re: Collision Detection Help
Post by: meishe91 on March 09, 2010, 11:51:57 pm
Haha only "slightly." But considering we're both band kids we're cool enough to do so ;)
Title: Re: Collision Detection Help
Post by: ztrumpet on March 10, 2010, 04:18:01 pm
*ZTrumpet points out that he is in marching band, and plays the (insert name of obvious instrument here). ;D

Does the code I posted earlier count as clever?  That's basically how Elmgon's movement is handled; I reorganize the code so it leaves at the loop, and checks stuff at the top. :)
Title: Re: Collision Detection Help
Post by: meishe91 on March 10, 2010, 04:30:19 pm
Woo go trumpet players!!!

I'd say it would, I still need to look over it to fully understand though.