From c832e790efc13de2f502e3677fb29eb1da574e4a Mon Sep 17 00:00:00 2001 From: Brian Caine Date: Mon, 28 Jan 2008 01:20:42 -0500 Subject: [PATCH] Whoo!!! Rotation around an arbitrary point is now working! After several long days of racking my brain, I have sprite rotation working. Yeah, so ... ... Awesome --- src/Sprite.cpp | 113 ++++++++++++++++++++++++++++++++++++++---------- src/Sprite.h | 6 --- src/test_suite/main.cpp | 23 ++++++++-- 3 files changed, 109 insertions(+), 33 deletions(-) diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 14d1f7e..df04375 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -107,6 +107,49 @@ void Sprite::draw(SDL_Surface* dest) return; } +int Sprite::getCurrentFrame() +{ + return cur_frame; +} + +int Sprite::getAngle() +{ + return sequence.angle; +} + +void Sprite::setAngle(int new_angle) +{ + sequence.angle = new_angle; + sequence.angle %= 360; + calcAngleOffset(); + return; +} + +std::vector Sprite::getPosition() +{ + return sequence.position; +} + +void Sprite::setPosition(std::vector new_position) +{ + if (new_position.size() < 2) + { + std::cerr << "Sprite::setPosition(): Not enough parameters for " + << "new position" << std::endl; + return; + } + + if (new_position.size() > 2) + { + std::cerr << "Sprite::setPosition(): Too many parameters for new " + << "position" << std::endl; + } + + sequence.position = new_position; + + return; +} + Sprite::Sprite(std::vector dframes, SpriteParams dparams) { was_made = true; @@ -201,8 +244,6 @@ void Sprite::makeNewAngleFrame(int angle) void Sprite::calcAngleOffset() { - std::vector new_offset; - int rot = -1; SDL_Surface* temp_surf = 0; @@ -218,35 +259,61 @@ void Sprite::calcAngleOffset() if (!temp_surf) return; - float x_1, y_1, x_2, y_2, radius, theta_1, theta_2; - float w_1, h_1, w_2, h_2; + float c_1[2], c_2[2], c_3[2]; + float center_1[2], center_2[2]; + float size_1[2], size_2[2]; + float focus_1[2], focus_2[2], focus_3[2], focus_4[2]; + float radius_1, radius_2, theta_1, theta_2, theta_3; + + size_1[0] = frames.at(cur_frame)->w; + size_1[1] = frames.at(cur_frame)->h; + + size_2[0] = temp_surf->w; + size_2[1] = temp_surf->h; + + focus_1[0] = sequence.focal_point.at(0); + focus_1[1] = sequence.focal_point.at(1); + + c_1[0] = 0; + c_1[1] = 0; + + center_1[0] = size_1[0] / 2; + center_1[1] = size_1[1] / 2; + + center_2[0] = size_2[0] / 2; + center_2[1] = size_2[1] / 2; + + radius_1 = sqrt( + static_cast(pow(center_1[0], 2) + pow(center_1[1], 2))); + theta_1 = acos(-center_1[0] / radius_1) * (180/PI); - x_1 = sequence.focal_point.at(0); - y_1 = sequence.focal_point.at(1); + c_2[0] = radius_1 * cos((theta_1 + rot) * (PI/180)); + c_2[1] = radius_1 * sin((theta_1 + rot) * (PI/180)); - w_1 = frames.at(0)->w; - h_1 = frames.at(0)->h; + c_3[0] = c_2[0] + center_2[0]; + c_3[1] = center_2[1] - c_2[1]; - radius = static_cast(sqrt( - pow((static_cast(x_1), static_cast(w_1) / 2), 2) + - pow((static_cast(y_1), static_cast(h_1) / 2), 2) )); + radius_2 = sqrt( + static_cast(pow(focus_1[0], 2) + pow(focus_1[1], 2))); + if (radius_2) + theta_2 = -acos(focus_1[0] / radius_2) * (180/PI); + else + theta_2 = 0; - theta_1 = (180 / PI) * acos(static_cast(x_1) / - static_cast(radius)); - theta_2 = theta_1 + rot; + theta_3 = theta_2 + rot; - x_2 = radius * cos(theta_2 * (PI / 180)) + (w_1 / 2); - y_2 = radius * sin(theta_2 * (PI / 180)) + (h_1 / 2); + focus_2[0] = radius_2 * cos(theta_3 * (PI/180)); + focus_2[1] = radius_2 * sin(theta_3 * (PI/180)); - w_2 = temp_surf->w; - h_2 = temp_surf->h; + focus_3[0] = c_3[0] + focus_2[0]; + focus_3[1] = c_3[1] - focus_2[1]; - new_offset.push_back(static_cast( - (x_2 - x_1) + ((w_1 / 2) - (w_2 / 2)))); - new_offset.push_back(static_cast( - (y_2, - y_1) + ((h_1 / 2) - (h_2 / 2)))); + focus_4[0] = focus_1[0] - focus_3[0]; + focus_4[1] = focus_1[0] - focus_3[1]; - angle_offset = new_offset; + angle_offset.clear(); + angle_offset.push_back(static_cast(focus_4[0])); + angle_offset.push_back(static_cast(focus_4[1])); return; } diff --git a/src/Sprite.h b/src/Sprite.h index bda9f1b..7490cd7 100644 --- a/src/Sprite.h +++ b/src/Sprite.h @@ -77,12 +77,6 @@ namespace fragrant int getAngle(); void setAngle(int new_angle); - void pause(); - bool getPaused(); - - int getFrame(); - void setFrame(int new_frame); - std::vector getPosition(); void setPosition(std::vector new_position); diff --git a/src/test_suite/main.cpp b/src/test_suite/main.cpp index d892a1d..b199d7b 100644 --- a/src/test_suite/main.cpp +++ b/src/test_suite/main.cpp @@ -41,12 +41,12 @@ const std::string FRAME_ONE = "frame1.png"; const std::string FRAME_TWO = "frame2.png"; const std::string FRAME_THREE = "frame3.png"; -const int SPRITE_POS[] = {100, 200}; +const int SPRITE_POS[] = {100, 100}; const std::string DATA = " battle_knucklessheet_stuff.png knuckles_sheet
10 150 40 40 knuckles_sheet punch1
50 150 40 40 knuckles_sheet punch2
90 150 40 40 knuckles_sheet punch3
140 150 40 40 knuckles_sheet punch3
180 150 40 40 knuckles_sheet punch4
220 150 40 40 knuckles_sheet punch5
punch punch1 punch2 punch3 punch4 punch5 0 100 1 100 2 100 3 100 4 100
"; const std::string ROOT = "../../relaxng/"; -const int SPRITE_ANGLE = 30; +const int SPRITE_ANGLE = 0; #if SDL_BYTEORDER == SDL_BIG_ENDIAN const int rmask = 0xff000000; @@ -106,10 +106,25 @@ int run(SDL_Surface* screen) std::vector sprites = createSprites(); std::vector::iterator iter; + int last_ticks = SDL_GetTicks(); + int cur_ticks = SDL_GetTicks(); + float angle = 1.0f; + while (running) { processEvents(&running); + last_ticks = cur_ticks; + cur_ticks = SDL_GetTicks(); + + angle += (static_cast(cur_ticks - last_ticks) / 1000.0f) * 10; + sprites.at(0)->setAngle(static_cast(angle)); + + if (angle > 360) + angle = static_cast(angle) % 360; + + std::cout << "angle is " << angle << std::endl; + for (iter = sprites.begin(); iter != sprites.end(); iter++) (*iter)->draw(screen); @@ -157,8 +172,8 @@ std::vector createSprites() parameters.position.push_back(SPRITE_POS[0]); parameters.position.push_back(SPRITE_POS[1]); - parameters.focal_point.push_back(30); - parameters.focal_point.push_back(10); + parameters.focal_point.push_back(64); + parameters.focal_point.push_back(64); parameters.repeat = true; parameters.angle = SPRITE_ANGLE; -- 2.11.4.GIT