Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: MRide on September 09, 2010, 10:36:39 pm

Title: Jumping - reverse gravity
Post by: MRide on September 09, 2010, 10:36:39 pm
Hello, I am trying to write a bit of code to simulate a character jumping.
I have done that.  Now what I want to do is have a way to reverse gravity when the user presses a button.  So the character will "fall" up and land on the ceiling.  I have only partially succeeded in this.  Hitting the button causes the character to float upward (or downward) until it is three spaces away from the floor/ceiling, and then it skips the next two spaces and lands.  I have been staring at my code and for some reason, cannot see the problem.  Help, please?

Also, here is my code (part of it came from meishe's jumping code)

Code: [Select]
ClrHome
7->X:Ans->S    //X is actually the Y coord., and S is the initial Y coord. that you jumped from
1->Y:Ans->B   //Y is the X coord. ,and B is either positive or negative one, depending on if the gravity is reversed
Delvar ARepeat 0  //A controls whether you move up, down, or not at all
getKey->K
If Ans or A!=0
Text(-1,7X,6(Y-1),"_    //one space
Y+(K=26)-(K=24
Ans-15((Ans=16)-not(Ans->Y
X-BA->X
A-2(X=(S-B3->A
If X=S
DelVar A
If not(A
X->S
B-2(K=31 and B=1)+2(K=31 and B=-1->C
If B!=C:Then
-1->A
C->B
If B=-1
1->S
If B=1
7->S
End
Text(-1,7X,6(Y-1),"X
A+(K=21 and X=S->A
rand(8
End

I realize this isn't very optimized.  That will be fixed.
Title: Re: Jumping - reverse gravity
Post by: DJ Omnimaga on September 09, 2010, 10:37:33 pm
Mhmm... Raylin might be great help there. He wrote a game involving inverting gravity
Title: Re: Jumping - reverse gravity
Post by: meishe91 on September 09, 2010, 10:43:05 pm
Well I haven't gone over all your code but that first If can be just If Ans I believe. Also, is that ClrHome supposed to be a ClrDraw?

Edit:
That first thing was wrong. I thought that "A" was an "Ans." In which case it'd be If Ans or A which I believe can be further optimized.
Title: Re: Jumping - reverse gravity
Post by: Raylin on September 09, 2010, 10:43:11 pm
Hold on...
Title: Re: Jumping - reverse gravity
Post by: MRide on September 09, 2010, 10:44:14 pm
Well I haven't gone over all your code but that first If can be just If Ans I believe. Also, is that ClrHome supposed to be a ClrDraw?

Yeah, like I said at the bottom of my post, this isn't optimized.  I'll go back over it when I get everything working.
Oh, yeah, it is.
Title: Re: Jumping - reverse gravity
Post by: Raylin on September 09, 2010, 10:52:51 pm
Okay. I was going mad.
This is my gravity reversing code.

Code: [Select]
If K=21:1-2(F=1→F
Have fun.
Title: Re: Jumping - reverse gravity
Post by: MRide on September 09, 2010, 11:05:14 pm
That doesn't work (At least, it doesn't do anything different).  I already have code that reverses the gravity. What I want to know is why the character skips two spaces.
Title: Re: Jumping - reverse gravity
Post by: meishe91 on September 09, 2010, 11:12:30 pm
I haven't narrowed it down yet, but still working on it, but I found something interesting. If you jump and then change the gravity it seems to work fine.
Title: Re: Jumping - reverse gravity
Post by: Raylin on September 09, 2010, 11:14:18 pm
Are you constantly adding the gravity variable to the Y-value?
If so, does it zero out on contact with the ground?
Title: Re: Jumping - reverse gravity
Post by: MRide on September 09, 2010, 11:27:11 pm
For the original code:
I essential have two variables that affect gravity.  The first is A, Which is 1, 0, or -1.  It is always subtracted from the y variable.
The second variable is B it controls whether you are up or down.  It is used to change the value of A and whether the y variable is allowed to be three more or less than the starting jump point.

If you use my code in the first post, everything works except the effect I have been describing.
Title: Re: Jumping - reverse gravity
Post by: Runer112 on September 09, 2010, 11:28:23 pm
Your problem exists in this line:
Code: [Select]
A-2(X=(S-B3->AWhen gravity is reversed and you are falling, A and B are both -1. When you hit X=4, this line thinks you have just hit the top of a jump and should start falling, so it subtracts 2, trying to turn A from 1 to -1. However, A is already -1, so A becomes -3, hence the 3-line jump. This can be fixed by changing the code to the following, which will simply set A to -1 if it thinks you have hit the top of a jump (even if you haven't and you were just falling):
Code: [Select]
If X=(S-B3
-1->A
Title: Re: Jumping - reverse gravity
Post by: MRide on September 09, 2010, 11:34:01 pm
Ah, I knew it had something to do with that line.  Thanks.