Hi all,
I`m having a small problem with gravity that I hope someone can help me with. In my game, a 2D platformer
I have an item in the sky which slowly falls to the ground gradually increasing in speed (gravity). When
the item hits an obstacle, in this case the ground, it stops. This worked fine when I used a simple
item\yposition=item\yposition+1 method, the item fell and rested on the ground when and where it hit it but
now I have added gravity the effect is ruined. The item still stops when it meets the ground but lands
slightly out, either just to high or just to low.
Here is what I`m using at the moment,
; create an items type.
type items
field xposition#,yposition#,fallspeed#,landed
end type
; set a constant rate for gravity.
const gravity#=0.1
; test the item for a collision.
item\collided=ImagesCollide(item\image,item\xposition,item\yposition,0,ground,groundxposition,groundyposition,0)
; if the item has collided with the ground then stop it otherwise gently increase its fallspeed by the constant
; gravity variable and add this to the items yposition to simulate simple gravity.
if item\collided=True
item\landed=true
else
item\yposition=item\yposition+item\fallspeed
item\fallspeed=item\fallspeed+gravity
endif
Like I said what happens is the item does stop when it hits the ground but it is off by a couple of pixels
which looks kind of ugly. This is obviously caused by the fallrate variable (er... I think) so I thought
that just subtracting the fallrate (at the time of impact from the items yposition) would cure this but it
doesn`t :( Maybe using floor or ceil would help round this floating point value to a whole value and cure
the problem but I`m not really sure how to implement it.
Any suggestions, Or other approaches?
Thanks,
Jason.
I`m having a small problem with gravity that I hope someone can help me with. In my game, a 2D platformer
I have an item in the sky which slowly falls to the ground gradually increasing in speed (gravity). When
the item hits an obstacle, in this case the ground, it stops. This worked fine when I used a simple
item\yposition=item\yposition+1 method, the item fell and rested on the ground when and where it hit it but
now I have added gravity the effect is ruined. The item still stops when it meets the ground but lands
slightly out, either just to high or just to low.
Here is what I`m using at the moment,
; create an items type.
type items
field xposition#,yposition#,fallspeed#,landed
end type
; set a constant rate for gravity.
const gravity#=0.1
; test the item for a collision.
item\collided=ImagesCollide(item\image,item\xposition,item\yposition,0,ground,groundxposition,groundyposition,0)
; if the item has collided with the ground then stop it otherwise gently increase its fallspeed by the constant
; gravity variable and add this to the items yposition to simulate simple gravity.
if item\collided=True
item\landed=true
else
item\yposition=item\yposition+item\fallspeed
item\fallspeed=item\fallspeed+gravity
endif
Like I said what happens is the item does stop when it hits the ground but it is off by a couple of pixels
which looks kind of ugly. This is obviously caused by the fallrate variable (er... I think) so I thought
that just subtracting the fallrate (at the time of impact from the items yposition) would cure this but it
doesn`t :( Maybe using floor or ceil would help round this floating point value to a whole value and cure
the problem but I`m not really sure how to implement it.
Any suggestions, Or other approaches?
Thanks,
Jason.