반응형

캐릭터가 마우스를 따라 회전 하도록 해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pygame
 
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640480))
clock = pygame.time.Clock()
 
class Player(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.image = pygame.image.load("player.png").convert()
        self.image.set_colorkey(self.image.get_at((00)))
        self.image = pygame.transform.scale(self.image,
                    (self.image.get_width()*1.5self.image.get_height()*1.5))
        self.original_image = self.image
        # 원본 이미지 보관.
        self.rect = self.image.get_rect(center=pos)
        self.pos = pygame.math.Vector2(pos)
        # pos를 벡터로 변환.
 
    def rotate(self):        
        direction = pygame.mouse.get_pos() - self.pos
        # 플레이어에서 마우스로의 벡터.
        radius, angle = direction.as_polar()
        print(angle)
        # radius는 마우스까지의 거리, angle은 방위각.
        # 오른쪽=0(-0), 아래=90, 왼쪽=180(-180), 위=-90
        self.image = pygame.transform.rotate(self.original_image, -angle+180)
        # 이미지를 회전하고 각도 보정. 이전 프레임의 회전된 이미지가 아닌
        # 원본 이미지를 이용한다.
        self.rect = self.image.get_rect(center=self.rect.center)
        # rect 재설정.
 
    def update(self):
        self.rotate()
 
def main():
    all_sprites = pygame.sprite.Group(Player(screen.get_rect().center))
 
    running = True
 
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False 
    
        all_sprites.update()
    
        screen.fill("black")
        all_sprites.draw(screen)
    
        pygame.display.flip()
        clock.tick(60)
 
    pygame.quit()
 
if __name__ == '__main__':
   main()
 

 

 

캐릭터가 마우스를 따라 회전한다.

 

반응형
Posted by J-sean
:

Rotation 회전

OpenGL 2019. 12. 13. 14:43 |
반응형

It describes how to rotate objects.

OpenGL에서 오브젝트 회전 하기.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <gl/glut.h>
 
GLfloat angle = 0.0f;
 
void MyDisplay();
void MyTimer(int value);
 
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("OpenGL Rotation");
 
    glutDisplayFunc(MyDisplay);
    glutTimerFunc(20, MyTimer, 1);
 
    glutMainLoop();
 
    return 0;
}
 
void MyDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    // 1st teapot, right, CCW
    glPushMatrix();
    glRotatef(45.0f1.0f0.0f0.0f);    // 45 degrees rotation around X-Axis to see the direction of the rotation.
    glTranslatef(0.5f0.0f0.0f);        // Changing the order of calling this Translate()..
    glRotatef(angle, 0.0f1.0f0.0f);    // and this Rotate() will affect how it spins. (rotation and revolution)
    glutWireTeapot(0.3);
    glPopMatrix();
    
    // 2nd teapot, left, CW
    glPushMatrix();
    // glPushMatrix pushes the current matrix stack down by one, duplicating the current matrix. That is, after a
    // glPushMatrix call, the matrix on top of the stack is identical to the one below it.
    glRotatef(45.0f1.0f0.0f0.0f);
    glTranslatef(-0.5f0.0f0.0f);
    glRotatef(-angle, 0.0f1.0f0.0f);
    glutWireTeapot(0.3);
    glPopMatrix();
    //glPopMatrix pops the current matrix stack, replacing the current matrix with the one below it on the stack.
 
    glutSwapBuffers();
}
 
void MyTimer(int value)
{
    angle += 1.0f;
    if (angle >= 360.0f) {
        angle = 0.0f;
    }
 
    glutPostRedisplay();
    glutTimerFunc(20, MyTimer, 1);
}



Run the program and see the directions of each rotation.


반응형
Posted by J-sean
:
반응형

Explains how to make and use a rotation matrix and a translation matrix with OpenCV. Below code shows how to rotate 20 degrees and translate 20 pixels along the x-axis and 60 pixels along the y-axis.


회전 행렬을 이용해 20도 회전(CW), 이동 행렬을 이용해 x축으로 20 pixel, y축으로 60 pixel 이동하는 방법입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char* argv[])
{
    vector<Point> rotation_source, rotation_destination;
    rotation_source.push_back(Point(20050));
    rotation_source.push_back(Point(40050));
    rotation_source.push_back(Point(400250));
    rotation_source.push_back(Point(200250));
    // Rotation Matrix
    float theta = 20 * (float)CV_PI / 180// 20 degrees rotation
    Matx22f r(cos(theta), -sin(theta), sin(theta), cos(theta));
    transform(rotation_source, rotation_destination, r);
 
    vector<Point3i> translation_source, translation;
    translation_source.push_back(Point3i(200501));
    translation_source.push_back(Point3i(400501));
    translation_source.push_back(Point3i(4002501));
    translation_source.push_back(Point3i(2002501));
    // Translation Matrix
    Mat t = Mat::eye(33, CV_8UC1);
    t.at<uchar>(02= 20// x-axis 20 pixels translation
    t.at<uchar>(12= 60// y-axis 60 pixels translation
    t.at<uchar>(22= 0;
 
    transform(translation_source, translation, t);
 
    vector<Point> translation_destination;
 
    for (int i = 0; i < translation.size(); i++)
        translation_destination.push_back(Point(translation[i].x, translation[i].y));
 
    // Draw source, rotation, translation
    Mat image(400500, CV_8UC3, Scalar(255255255));
    for (int i = 0; i < 4; i++)
    {
        line(image, rotation_source[i], rotation_source[(i + 1) % 4], Scalar(000), 2);
        line(image, rotation_destination[i], rotation_destination[(i + 1) % 4], Scalar(25500), 1);
        line(image, translation_destination[i], translation_destination[(i + 1) % 4], Scalar(00255), 1);
    }
 
    imshow("image", image);
    waitKey(0);
 
    return 0;
}
cs





반응형
Posted by J-sean
: