Link6415 Posted November 7, 2016 Share Posted November 7, 2016 *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. Quote Link to comment Share on other sites More sharing options...
JamesD Posted November 7, 2016 Share Posted November 7, 2016 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. Quote Link to comment Share on other sites More sharing options...
+5-11under Posted November 7, 2016 Share Posted November 7, 2016 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. Quote Link to comment Share on other sites More sharing options...
RevEng Posted November 7, 2016 Share Posted November 7, 2016 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. Quote Link to comment Share on other sites More sharing options...
TMR Posted November 10, 2016 Share Posted November 10, 2016 (edited) 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 November 10, 2016 by TMR Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted November 11, 2016 Share Posted November 11, 2016 Actually it's dead simple. Quote Link to comment Share on other sites More sharing options...
Link6415 Posted November 12, 2016 Author Share Posted November 12, 2016 (edited) Thanks! This all helped. I used subpixel position and adjusted my timing to get it just right. Edited November 12, 2016 by Link6415 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.