Jump to content
IGNORED

making gravity in assembly - a nightmare


Link6415

Recommended Posts

*Sigh*

 

I've been trying to make gravity in my game for about a week now, and it's driving me nuts.

 

All I want to do is throw a sprite up into the air and have it fall back down - while simulating gravity, so it looks real. I've been playing a lot of Bob Smith's "Video Pinball" on the 2600 to get inspiration:

 

http://www.free80sarcade.com/2600_Video_Pinball.php

 

 

My current method is this:

 

A = 1, B = 16

 

Wait for A tic and move sprite. Repeat B times.

Increase A. Decrease B.

Repeat until B = 0, then flip A and B and move the sprite in the opposite direction.

 

This works, the sprite gets slower as it gets higher, and speeds up as it falls, but it looks really bad. There must be a better way.

 

Some pseudocode would be great, I'm going insane. :mad:

Link to comment
Share on other sites

The acceleration of gravity is 9.8 meters per second per second downwards.
Rounding that to ten makes the math easier.
Build a table that approximates that from initial velocity upwards to downwards.
Then step through the table to adjust the speed every so many frames.
Maybe have a speed and how many frames to operate at that speed before advancing to the next location in the table.

Link to comment
Share on other sites

For Y axis:

 

accel = gravity = some integer (say -4 (down)).

set a velocity. If it's a throw upward, say +20 (up) when thrown.

you have a start position say y=0 (bottom - may need to reverse if top is at zero).

 

So at the point of a throw

accel = -4, vel =20, y = 0, then after very frame...

vel = vel + accel, for instance vel = 20 + -4 = 16, position = position plus vel, for instance y = 0 + 16 = 16.

continue above for each frame.

decide what to do when item falls to floor again.

 

if x axis is used too, accel = 0, and set an initial velocity.

Link to comment
Share on other sites

Start off with subpixel positioning to move the object at a variable rate. That link is for the 2600, but the subposition code and concept is generic.

 

To simulate the pull of gravity, you also need variables for X and Y velocity. They should be 16-bit like the subpixel X and Y positioning. Every pass of the main loop, your X velocity will be added to the X position, and Y velocity will be added to the Y position.

 

After that gravity is easy. It's just a constant downward force, so every frame you subtract a constant amount from the Y velocity. If you want the object to shoot upward, you assign the Y velocity a large constant amount. If you want to simulate thrust, you add smaller amounts to the velocity variables.

 

It's up to you to tweak the constants, to simulate more or less gravity, thrust, etc.

Link to comment
Share on other sites

Pseudo code:

 

YSpeed=-5

YTimer=0

 

[wait for VBL]

SpriteY=SpriteY+YSpeed

 

YTimer=YTimer+1

If YTimer=5 ; change this to alter the strength of "gravity" to taste

YSpeed=YSpeed+1

YTimer=0

Endif

 

(Been using BlitzMax a lot recently, so there's an Endif and i had to fight myself to avoid things like YTimer:+1 !)

 

In 6502:

lda #$00

sta YTimer

lda #$fb

sta YSpeed

 

[VBL wait]

lda SpriteY

clc

adc YSpeed

sta SpriteY

ldx YTimer

inx

cpx #$05 ; change this to alter the strength of "gravity" to taste

bne skip

inc YSpeed

ldx #$00

skip stx YTimer

 

A limiter to stop it getting past a maximum positive value is a good idea too... =-)

Edited by TMR
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...