3 registered members (AndrewAMD, VoroneTZ, dpn),
1,346
guests, and 10
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
bounce + low framerate ??
#472238
04/17/18 21:43
04/17/18 21:43
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
OP
Senior Expert
|
OP
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hi! Can anyone please tell me, why does bounce go wrong when framerate is low? Please, take a look at this example (my attempt to create bouncing grenade):
#include <acknex.h>
#include <default.c>
#define PRAGMA_POINTER
// default trace/move flags
#define TRACE_FLAGS (IGNORE_ME | IGNORE_PASSABLE | IGNORE_PASSENTS | IGNORE_MAPS | IGNORE_SPRITES | IGNORE_CONTENT)
#define MOVE_FLAGS (IGNORE_ME | IGNORE_PASSABLE | IGNORE_PASSENTS | IGNORE_MAPS | IGNORE_SPRITES | IGNORE_CONTENT)
action grenade(){
c_setminmax(my);
set(my, SHADOW | POLYGON);
VECTOR velocity;
vec_set(&velocity, vector(25, 0, 5));
vec_rotate(&velocity, &camera->pan);
var _soil_height = 0; // target height from surface bellow
var _gravity = 5; // 1 - 10 (default 5)
var _restitution = 0.5; // 0.1 - 0.9 (default - 0.25)
var counter = 0;
while(my){
// still moving ?
if(vec_length(&velocity) > 1){
VECTOR temp;
vec_set(&temp, &velocity);
vec_scale(&temp, time_step);
c_move(my, nullvector, &temp, MOVE_FLAGS);
velocity.z -= _gravity * time_step;
if(HIT_TARGET){
var speed = vec_length(&velocity);
vec_set(&velocity, &bounce);
vec_normalize(&velocity, _restitution * speed);
}
}
else{
// reset all movement
vec_fill(&velocity, 0);
}
DEBUG_VAR(velocity.x, 200);
DEBUG_VAR(velocity.y, 220);
DEBUG_VAR(velocity.z, 240);
DEBUG_VAR(bounce.x, 300);
DEBUG_VAR(bounce.y, 320);
DEBUG_VAR(bounce.z, 340);
counter += time_frame / 16;
if(counter >= 3){ break; }
wait(1);
}
safe_remove(my);
}
void main(){
shadow_stencil = 2;
fps_max = 60;
warn_level = 6;
level_load("");
wait(3);
camera->arc = 90;
vec_set(&camera->x, vector(-439, 0, 323));
vec_set(&camera->pan, vector(0, -36, 0));
level_ent = ent_create(CUBE_MDL, nullvector, NULL);
vec_set(&level_ent->scale_x, vector(256, 256, 0.1));
c_setminmax(level_ent);
set(level_ent, POLYGON | SHADOW);
ENTITY *wall = ent_create(CUBE_MDL, vector(0, 256, 64), NULL);
vec_set(&wall->scale_x, vector(4, 8, 8));
c_setminmax(wall);
set(wall, POLYGON | SHADOW);
int i = 0;
var counter = 0, throw = 0;
while(!key_esc){
if(mouse_left){
if(throw == 0){
ent_create(CUBE_MDL, &camera->x, grenade);
throw = 1;
}
}
if(throw == 1){
counter += time_frame / 16;
if(counter > 0.5){ throw = 0; counter -= 0.5; }
}
// slow down framerate for testing
if(key_e){ fps_max = 20; }
else{ fps_max = 60; }
DEBUG_VAR(fps_max, 0);
wait(1);
}
}
As you can see, it starts moving towards the left, slowly... Why does that happen? Also, there is always small difference in grenade's movement, on different framerate (I guess it's framerate independent, but there is still a difference!). Any help would be appreciated! Best regards!
|
|
|
Re: bounce + low framerate ??
[Re: 3run]
#472247
04/18/18 05:37
04/18/18 05:37
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Regarding the frame rate problem: I didn't go through your code in detail but one thing you have to make sure is that you're using time and calculating other physical quantities correctly. Here's an example for a while loop:
Force = ... Acceleration = Force / Mass Velocity += Acceleration * DeltaTime Position += Velocity * DeltaTime
In your case you can leave out the acceleration part and modify the velocity manually.
Edit: so I took a look at your code and it seems to follow the basic principle, I guess the problem is somewhere else.
Also what do you mean by "goes wrong"?
Last edited by Kartoffel; 04/18/18 06:20.
POTATO-MAN saves the day! - Random
|
|
|
Re: bounce + low framerate ??
[Re: Kartoffel]
#472250
04/18/18 08:04
04/18/18 08:04
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
OP
Senior Expert
|
OP
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hi Kartoffel! By 'goes wrong' I mean that after moving in a straight line, box starts sliding towards the left, if frame rate is low (just run the code above, hold E and press mouse left). You can see those numbers on the screen, they go for 'velocity.x, y, z and bounce.x, y, z. Y should be equal to zero all the time, but on low frame rate it slightly changes.. As for the frame rate problem, I can't understand why, but it's still there.. Box will land on the different distance away from the camera, if I change fps from 60 to 20/30. Greets. Edit: I'll try to explain on the picture as well
Last edited by 3run; 04/18/18 08:12.
|
|
|
Re: bounce + low framerate ??
[Re: 3run]
#472251
04/18/18 08:31
04/18/18 08:31
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
well I think this much of a difference in distance is caused by the inaccuracy of the simulation at a lower framerate (just like the case where the jump arc can turn out a bit lower when games run at very low fps)
Why it starts moving left I don't know, it's probably continuously bouncing. Might also be related to inaccuracies (standard lite-c vectors only use var).
You could try switching to float vectors or calculate the bounce vector yourself, not sure if thaat helps, though.
POTATO-MAN saves the day! - Random
|
|
|
Re: bounce + low framerate ??
[Re: 3run]
#472253
04/18/18 08:55
04/18/18 08:55
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
oh, it's the polygon collisions.
I started completely avoiding c_trace and c_move. using an actual physics engine will probably give you a more solid solution, and in general a better collision detection / scene query (player movement, etc.)
POTATO-MAN saves the day! - Random
|
|
|
Re: bounce + low framerate ??
[Re: Kartoffel]
#472255
04/18/18 09:03
04/18/18 09:03
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
OP
Senior Expert
|
OP
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
I started completely avoiding c_trace and c_move. using an actual physics engine will probably give you a more solid solution, and in general a better collision detection / scene query (player movement, etc.) native physX implementation sucks.. so I have to use OBB.. edit: I still hope someone will be able to open my eyes on this.
Last edited by 3run; 04/18/18 09:04.
|
|
|
|