Understanding (and reversing) gravity [4.5.2]

jataka5000

New member
Hello,
I've finished all the platformer tutorials (up through tutorial 13), and I'm diving deeper to try and understand how gravity really works in the NESmaker doHandlePhysics_Gravity subroutine.
So far, I've figured out that Object_v_speed_hi and Object_v_speed_lo are the major and minor (respectively) numbers that tell you what your speed is. It seems that a positive value means falling downward.
I'd like to understand the asm code for the gravity part (e.g. doHandlePhysics_Gravity_LeftBoundsSolid_vulnerabilityBit_oneWayPlat_prizeBlock.asm lines 374 onward) to see how I could reverse the effect of gravity -- that is, all objects would tend to move upward instead of down.
It seems that
Code:
ADC #GRAVITY_HI
adds the gravity acceleration value to Object_v_speed_hi until it reaches the max speed. I thought that
Code:
SBC #GRAVITY_HI
would subtract the gravity acceleration value from Object_v_speed_hi but I don't think I was right.

Perhaps the issue is in line 474:
Code:
BNE notOverFallSpeed
	;; is at least max fall speed.
	LDA #$00
	STA Object_v_speed_lo,x
	notOverFallSpeed:

A large part of my confusion is that I don't know what combinations of Object_v_speed_hi and Object_v_speed_lo correspond to negative or positive velocities (i.e. going up vs down)... Also, it seems that BNE doesn't only check to see if one value is not equal to the other, but whether one value is larger than the other.

Greatly appreciate any help in deciphering what is happening with the code. Let's defy gravity! :) Well, not just defy but make it turn the opposite direction, for starters...

-j5k
 

jataka5000

New member
Update: I am making progress in reversing gravity in the platformer tutorial! http://www.youtube.com/watch?v=XRoawsGyQI0

here's what I did that led to results:

First, I changed MAX_FALL_SPEED to be negative in doHandlePhysics_Gravity:

Code:
;; Handle Gravity.
;;;;;;;;;;;;;;; Change these constants to change gravity's behavior.
MAX_FALL_SPEED = #$FE  ; $#FF - 80 means negative numbers... speed is positive....
GRAVITY_LO = #$02
GRAVITY_HI = #$00

then, a bit lower, I made it so that the gravity parameters are *subtracted* instead of added from the current vertical speed:

Code:
                        LDA Object_v_speed_lo,x
			CLC
			SBC #GRAVITY_LO
			STA Object_v_speed_lo,x
			LDA Object_v_speed_hi,x
			SBC #GRAVITY_HI
			STA Object_v_speed_hi,x

Alternately, for just constant speed behavior (not as cool), you could just write:
Code:
	                LDA #$80  
			STA Object_v_speed_lo
			LDA #$ff
			STA Object_v_speed_hi
If you want to jump when in contact with a solid surface, jump_throughPlat.asm needs an update:

Code:
    LDA Object_y_hi,x
    CLC
    sbc #$02  ;;;;;;;;;;; changed from ADC
    CLC
    sbc temp2  ;;;;;;;;;;;; changed from ADC
    STA temp1

This mostly works. I did find that sometimes I can't jump (especially after I moved a screen over).

Biggest problem at the moment: when approaching a solid tile from above, I am somehow pushed through to the other side, not stopped above the tile. I need to understand better how collisions with solid objects are checked. As mentioned in the youtube video linked above, there are pieces that are not clear to me about what's going on with the check collisions code.

I hope that this is helpful to anyone who is getting started in manipulating variables in the asm.

Grateful for any suggestions!

Cheers,
j5k
 

mouse spirit

Well-known member
Really great. Nice video too. I did comment on the vid but i'll say it here too.
This is what it seeeeeems like to me untill someone else can answer better. The players top of hitbox"head area" is checked by the bottom of a solid tile. The players bottom of hitbox "feet area" are checked by the top of a solid tile.
Which maybe is why it kinda but doesnt work in the video.
Are you actually fliiping the player? Or is it just a graphic that looks upside down. That would make a difference in what i said.

And it may be easier to act like gravity is flipped(like just reverse up n down gravity and have some player states that have upside down graphics., but i see youre really goin for it all here. Well done and keep on.
 

jataka5000

New member
Thanks so much all!
mouse spirit, I only flipped the graphic so that it looks upside down. I like your suggestion about checking to make sure that the "feet area" are being checked by the top of a tile. That might be an issue... Maybe in the doHandlePhysics_gravity subroutine, also possibly in stopOnSolid_Platformer... or even doHandleTileCollisions_hScroller.. I'm guessing that there might be some conflicting commands happening between the two, as they aren't completely separate.
 

mouse spirit

Well-known member
Totally! Hey what i did in gamemaker was i reversed the numbers gravity ofcourse,then reversed the way jump worked, lastly i made sure that collisions worked right and made sprites flip.Probly what you are doing and i make it sound easy but as you know its not so simple all the time!
I then made a switch to run normal grav code or reversed grav code when i wanted. Worked great but i would not know how to do that with nesmaker sadly.
 

jataka5000

New member
Gotcha. This one does seem to be tricky. That's exactly where I'm heading -- switch blocks etc will change gravity, then various weird puzzles etc will ensue :) I hope I can track it all down... this one is definitely messing with me! arrgghhh
 

mouse spirit

Well-known member
Ok, new guess mixxed with my old one. Maybe specifically when you jump, it is checkin for a collision on your head as to not jump up thru blocks,never at your feet.(for a normal standing person) but you are jumping in reverse so when you are reverse jumping maybe you have to say my head is down here. Sounds like what i said,i know.

But check your jump code, check those physic bytes, and if you are actually triggering jumpthrough one way block code also.
 
Top Bottom