I've made the changes to the functions and now they work great! However I still have one problem. On every frame at the moment, the effects of gravity are applied to the sprites speed. Obviously as these effects are based on frame rates it makes sprites appear to be moving at different speeds on different computers. However as these functions also handle collisions I dont see any other alternative but to call these functions every frame.
Below is my code for the objects vertical physics which also tests for collisions underneath the character.
The thinking behind this function is:
if players going upwards:
set onfloor flag to false
decrease yspeed by multiplying it by 0.91
if yspeed is very small, set to 0. Otherwise we would never get to 0!
(ignore collision possibility if players going upwards)
if players going downwards:
if the players not colliding with anything:
set onfloor flag to false
set speed to 0.5 if speed was 0 so multiplying will actually change something
if speed is less than 5, multiply it by 1.1
Anyway im sure theres a fairly simple solution using the difference in times instead of just constant values of 0.91 and 1.1 but am struggling to figure this out for myself
Code:
procedure TPlayerChar.VPhysics(map:TMap);
begin
if yspeed<0 then
begin
onfloor:=false;
yspeed:=yspeed*0.91;
if yspeed>-0.5 then yspeed:=0;
end else
if yspeed>=0 then
if not collisions.VCollision(map,getys(currenttime,lasttime,yspeed),xpos,ypos) then
begin
onfloor:=false;
if yspeed=0 then yspeed:=0.5 else
if yspeed<5 then yspeed:=yspeed*1.1;
end else
begin
onfloor:=true;
yspeed:=0;
end;
end;
Any comments on this code, ie. if im doing something a bit silly, are more than welcome!
Thanks!
Bookmarks