Wie repariere ich Jitteriness, wenn ich diagonal bewegt?C++

Programme in C++. Entwicklerforum
Anonymous
 Wie repariere ich Jitteriness, wenn ich diagonal bewegt?

Post by Anonymous »

In meinem Spiel (C ++ /Raylib), wenn ich den Player diagonal bewege, gibt es für den Rest der Elemente
den Charakter, den ich "scrollen" (fügt einen Offset einen Versatz hinzu. Ein mathematischer 2D -Vektor), und dann zeichnet sich die Elemente auf. glatt ohne Jittering, aber wenn sie diagonal bewegt wird, wird die Schriftrolle vor dem Zeichnen aktualisiert, nicht während der oder danach nicht zu sehr mit der Art und Weise, wie die Scroll angewendet wird, der Vektor, der den Scroll -Offset darstellt

Code: Select all

//the move function
inline bool DynamicLevelObject::move(const dvec2 amt, bool should_scroll) {
//returns true if we moved at all
rect future = collision;
future.x += amt.x;
collision_hit hit;
moving = false;
if (amt.x != 0 && (hit = Level->colliding(future, &collision)).max_collider_status != collisionType::BLOCK_ALL) {
moving = true;
on_ground = hit.walkable;
collision.x += amt.x;
last_movement.x = amt.x;
} else {
moving = false;
future.x -= amt.x;
last_movement.x = 0;
}
future.y += amt.y;
if ((amt.y != 0) && (hit = Level->colliding(future, &collision)).max_collider_status != collisionType::BLOCK_ALL) {
moving = true;
on_ground = hit.walkable;
collision.y += amt.y;
last_movement.y = amt.y;
} else {
last_movement.y = 0;
}
move_dir = vtod(last_movement);

if (on_ground) {
last_valid_pos = collision.pos();
}

if (should_scroll) Level->focusScroll(collision.center());

return moving;
}

//in the level class
void draw(dvec2 offset) override {
for (const auto& obj: objects) {

obj->draw(scroll);

}
}

//the player draw function, most of the other object draw functions are very similar
//to this, I severely doubt it has anything to do with the animations

void draw(const dvec2 offset) override {
sprite::drawShadow({collision.center().x-offset.x-1, collision.y+collision.h-offset.y}, collision.w/2.5f, 0.8f*collision.h);
DrawAnimation(*current_animation, collision.pos()-offset-dvec2{5, 25});
}
< /code>
Ich habe versucht, die Schriftrolle abzurunden, die Position der Spieler abzurunden, alles abzurunden und im Grunde alles, was mit der Rundung zusammenhängt, mit unterschiedlichem Erfolg und vielen vielen zusätzlichen Problemen, die mit dem Runden verbunden sind (z. B.  massiv langsamer Bewegung, Jittery Player, seltsames Bewegungsverhalten usw.).#include "raylib.h"
#include 
#include 
#include 

struct dvec2 {
double x;
double y;

dvec2 operator+(const dvec2& other) {
return {x + other.x, y + other.y};
}

dvec2& operator +=(const dvec2& other) {
x += other.x;
y += other.y;

return *this;
}
};

void focusScroll(dvec2& scroll, dvec2 pos) {
scroll = {pos.x-640, pos.y-360};
}

void normalize_vec(dvec2& vec) {
const double len = sqrt((vec.x * vec.x) + (vec.y * vec.y));
vec.x /= len;
vec.y /= len;
}

int main() {
InitWindow(1280, 720, "main");

dvec2 player_pos = {0, 0};
dvec2 scroll = {0, 0};
focusScroll(scroll, player_pos);
//replace with any 32*32 pixel texture, helps to see the jitter
Texture floor = LoadTexture("FloorTiles.png");

SetTargetFPS(60);

while (!WindowShouldClose()) {

dvec2 movement = {0, 0};

if (IsKeyDown(KEY_A)) {
movement.x = -1;
}else if (IsKeyDown(KEY_D)) {
movement.x = 1;
}

if (IsKeyDown(KEY_W)) {
movement.y = -1;
}else if (IsKeyDown(KEY_S)) {
movement.y = 1;
}

//normalize movement
if (movement.x != 0 || movement.y != 0) {
normalize_vec(movement);

//3pixels per frame, roughly the actual speed of the character in the game
movement.x *= 3;
movement.y *= 3;

player_pos += movement;
scroll += movement;
}

BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(floor, {0, 0, 512, 512},
{static_cast(-scroll.x), static_cast(-scroll.y), 512, 512}, {0, 0},
0.0, WHITE);
DrawCircle(static_cast(player_pos.x-scroll.x), static_cast(player_pos.y-scroll.y), 20.0, RED);

DrawText((std::string("Scroll: ") + std::to_string(scroll.x) + ", " + std::to_string(scroll.y)).c_str(), 20, 20, 20, WHITE);

EndDrawing();

}
}
sollte in der Lage sein, den Jitter zu sehen, wenn der Spieler diagonal bewegt wird und wie sonst es etwas verschwommen sein kann, aber insgesamt ziemlich in Ordnung

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post