Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: leafy on December 27, 2010, 12:07:59 pm

Title: Gravitational Acceleration and Platform Detection
Post by: leafy on December 27, 2010, 12:07:59 pm
When I make an object fall at an exponential rate as if gravity were affecting it, it sometimes misses the platform and passes right through it. I know it's a problem with gaps between y values for adjacent x values, but how do you fix it?
Title: Re: Gravitational Acceleration and Platform Detection
Post by: Deep Toaster on December 27, 2010, 02:12:38 pm
Yeah, pretty common problem with platformers. An easy way to fix it is to set up a For( loop each pass to detect blocks. For example, if you have your X value stored in X, Y value stored in Y, and vertical velocity stored in V, do something like

Code: (Axe) [Select]
:If V>0
:For(I,1,V)
:If pxl-Test(X,Y+I+7)+pxl-Test(X+7,Y+I+7)
:Y+I→Y
:0→V
:Else
:Y+V→Y
:End
:End
:End

The pxl-Test(s detect blocks, but you can easily change that if you use other forms of collision detection (like with a tilemap). The Y+I+7 detects blocks starting eight pixels after the character, assuming the character is eight pixels high (you can change this too). Same with the X+Y in the second pxl-Test(: it assumes the character is eight pixels wide and detects blocks at its right side.
Title: Re: Gravitational Acceleration and Platform Detection
Post by: Binder News on December 27, 2010, 03:01:16 pm
My suggestion would be to put that in a subroutine and have it return at what point it found a collision. That way you could set V to 0, and increase the Y by the returned amount.
Title: Re: Gravitational Acceleration and Platform Detection
Post by: leafy on December 27, 2010, 04:29:13 pm
aight, I edited my program and stuck it into a subroutine, but it's still not working.
I added a g variable because my program will need to deactivate gravity later on.

EDIT: Never mind, I lied, I didn't figure it out.
Title: Re: Gravitational Acceleration and Platform Detection
Post by: Michael_Lee on December 27, 2010, 07:36:49 pm
I might be completely wrong, but I think part of your problem was that in your pxl-Tests (in your subroutine, PLT): every time in the loop the pxl-Test was false, you added (or subtracted) the velocity from your y-coordinates.  This meant that your sprite would go much futher then you wanted.

(Also, I'm assuming pgrmALEV drew some kind of floor or something, so I replaced it with a line command) to demonstrate.

I added your code (optimized):
Code: [Select]
.HOGGY

[007E425A5A427E00]→Pic1

1→L→F→G→V+4→Y*5→X

ClrDraw
Line(1,60,90,60)
Pt-Off(X,Y,Pic1)

.Main loop
While 1
Text(1,1,G►Dec)
Pt-Change(X,Y,Pic1)

If getKey(15)
ClrDraw
Return
End

.Control movement
getKey(3)-getKey(2)+X→X

.Gravity Acceleration (this doesn't need to be in a subroutine)
If G=1
V+1→V
End

.Collision test
Sub(PLT)

Pt-On(X,Y,Pic1)
DispGraph
 
End

.Subroutines begin here
Lbl PLT
If V
For(I,1,V)
If pxl-Test(X+1,Y+I+5) or pxl-Test(X+6,Y+I+5)
Y+I-2→Y
0→V
0→G
Return
.If something is hit, the program returns to the main loop
End
End
.If I went through the For loop without hitting anything...
If F         .(I'm assuming that F can only be 1 or 0 here)
Y+V→Y
Else
Y-V→Y
End
End
Return

Title: Re: Gravitational Acceleration and Platform Detection
Post by: fb39ca4 on December 30, 2010, 09:42:26 pm
This might not be what you wanted in your game, but doesn't gravity increase velocity quadratically?
Title: Re: Gravitational Acceleration and Platform Detection
Post by: leafy on December 30, 2010, 09:59:51 pm
Yeah. In my final version, i made the acceleration equal to t^2, which seems to work quite well. If you wanted to slow it down or speed it up, make it gt^2.

I thought the code was buggy because the exponent was too rapid, so i made it a "linear" increase in acceleration.
Title: Re: Gravitational Acceleration and Platform Detection
Post by: ztrumpet on December 30, 2010, 10:26:38 pm
Make sure that you cap downward motion so that it only moves a max of one frame per second. :)
Title: Re: Gravitational Acceleration and Platform Detection
Post by: Builderboy on December 31, 2010, 01:44:35 am
This might not be what you wanted in your game, but doesn't gravity increase velocity quadratically?

Acceleration is constant, Velocity is changed linearly, and Position is changed quadratically :) The best way i have found to do acceleration is to have a velocity variable and add your acceleration to the velocity every frame, and then add the velocity to the position every frame.
Title: Re: Gravitational Acceleration and Platform Detection
Post by: Broseph Radson on January 01, 2011, 05:12:02 pm
Maybe make it so that if your sprite goes below the platform it automatically resets its height to be on the platform. If Y>([platform height]-8):([platform height]-8)->Y:End
Title: Re: Gravitational Acceleration and Platform Detection
Post by: shmibs on January 01, 2011, 06:45:41 pm
/\ that works with tile by tile collisions as long as the terminal velocity is less than the height of a tile(and it's a very good way to keep BASIC platformers running at a decent speed). the for loop method is still necessary for collisions on a pixel by pixel basis, however.