Author Topic: Gravitational Acceleration and Platform Detection  (Read 16378 times)

0 Members and 1 Guest are viewing this topic.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Gravitational Acceleration and Platform Detection
« 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?
In-progress: Graviter (...)

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Gravitational Acceleration and Platform Detection
« Reply #1 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.
« Last Edit: December 27, 2010, 02:14:33 pm by Deep Thought »




Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: Gravitational Acceleration and Platform Detection
« Reply #2 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.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Gravitational Acceleration and Platform Detection
« Reply #3 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.
« Last Edit: December 27, 2010, 04:46:15 pm by leafiness0 »
In-progress: Graviter (...)

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Gravitational Acceleration and Platform Detection
« Reply #4 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

My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Gravitational Acceleration and Platform Detection
« Reply #5 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?

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Gravitational Acceleration and Platform Detection
« Reply #6 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.
In-progress: Graviter (...)

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Gravitational Acceleration and Platform Detection
« Reply #7 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. :)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Gravitational Acceleration and Platform Detection
« Reply #8 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.

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
Re: Gravitational Acceleration and Platform Detection
« Reply #9 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

Offline shmibs

  • しらす丼
  • Administrator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2132
  • Rating: +281/-3
  • try to be ok, ok?
    • View Profile
    • shmibbles.me
Re: Gravitational Acceleration and Platform Detection
« Reply #10 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.