Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - igor

Pages: [1]
1
Python / Re: Transformation issue
« on: July 08, 2013, 08:53:20 am »
Success! I was able to make Animation class which inherited from sfml.TransformableDrawable, and it work right! I use standart  draw method, but states.transform.combine(self.transformable.transform) replacing states.transform.combine(self.transform):
def draw (self, target, states):
        states.transform.combine(self.transform)
        target.draw(self._sprite, states)
 

2
Python / Re: Transformation issue
« on: July 06, 2013, 05:56:30 am »
Fix: both Laurent fixed draw method and draw method with manual copying, no longer produce a crash, but animation still drawn at left top corner.

Update: crashes don't occur only when I run script under IDLE.

Manual copying version:
def draw (self, target, states):
        def copy_renderstates(renderstates):
                ret = sf.RenderStates()
                ret.blend_mode = renderstates.blend_mode
                ret.shader = renderstates.shader
                ret.texture = renderstates.texture
                ret.transform = renderstates.transform
                return ret
        new_states = copy_renderstates(states)
        new_states.transform.combine(self.transformable.transform)
        target.draw(self._sprite, states)
 

Laurent fixed version:
def draw (self, target, states):
    states.transform.combine(self.transformable.transform)
    target.draw(self._sprite, states)
 

Update: I remade Animation class in sf.Transformable in an internal attribute style, and it's work, but looks ugly... Thus it's a bug most probably in the sf.TransformableDrawable.

3
Python / Re: Transformation issue
« on: July 05, 2013, 03:54:58 pm »
Yes, but in the C++ documentation:

 
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const

states passed by value, but in Python all arguments passed by reference and states must be saved.
But if I make copy of states it's don't work (just black window, without render):
def draw (self, target, states):
        new_states = copy.copy(states)
        new_states.transform.combine(self.transformable.transform)
        target.draw(self._sprite, new_states)
 
By the way fixed version, also don't work, moreover after few frames program freeze and crash.:
def draw (self, target, states):
        states.transform.combine(self.transformable.transform)
        target.draw(self._sprite, states)
 
Here code of animation.py module:
import sfml as sf

class Animation(sf.TransformableDrawable):
        _timer = sf.Clock()
        def __init__ (self, sprite, duration):
                sf.TransformableDrawable.__init__(self)
                self._sprite = sprite
                self._frame = 1
                self._frames_count = sprite.texture.width // sprite.texture.height
                self._frame_size = sprite.texture.height
                self._sprite.texture_rectangle = sf.Rectangle((0, 0), (self._frame_size, self._frame_size))
                self._rate = duration // self._frames_count
                self.is_run = False
        def start (self):
                self._last_time = Animation._timer.elapsed_time.milliseconds
                self._frame = 1
                self.is_run = True
        def stop (self):
                self.is_run = False
        def update (self):
                if self.is_run:
                        # set number of renderable frame
                        current_time = Animation._timer.elapsed_time.milliseconds
                        elapsed_time = current_time - self._last_time
                        if elapsed_time >= self._rate:
                                offset = (elapsed_time // self._rate) % self._frames_count
                                self._frame = (self._frame + offset) % self._frames_count      
                                self._last_time = current_time
                        # set renderable frame on sprte
                        frame_pos = (self._frame_size*self._frame-1, 0)
                        frame_size = (self._frame_size, self._frame_size)
                        self._sprite.texture_rectangle = sf.Rectangle(frame_pos, frame_size)
        def draw (self, target, states):
                states.transform.combine(self.transformable.transform)
                target.draw(self._sprite, states)
 
Code of main script:
import sfml as sf
from animation import Animation

def main ():
        wnd = sf.RenderWindow(sf.VideoMode(800, 800), "Anim test")
        texture = sf.Texture.from_file("boxes.png")
        sprite = sf.Sprite(texture)
        animation = Animation(sprite, 1000)
        animation.position = (400, 400)
        animation.start()
        while wnd.is_open:
                for event in wnd.events:
                        if type(event) is sf.CloseEvent:
                                wnd.close()
                wnd.clear(sf.Color.BLACK)
                animation.update()
                wnd.draw(animation)
                wnd.display()

if __name__ == '__main__':
        main()
 

4
Python / Transformation issue
« on: July 05, 2013, 01:33:58 pm »
I'm try to implement animation class via sfml.TransformableDrawable. In main script I wrote animation.position = (400, 400), but nothing happen with render position. I think that my implementation of draw method is wrong, but I don't know how make it right. Yes, documentation  explains how drawable and transformable linked with each other, but it is still  the dark forest for me.

Here my draw method implementation:

def draw (self, target, states):
        # argument by reference -> states will be modified !!!
        states.transform.combine(self.transformable.transform)
        target.draw(self._sprite)
 

P.S.: sorry I'm  accidentally added poll.

5
Graphics / Re: Rotate points via Transform
« on: January 09, 2013, 07:11:17 pm »
I hoped that sf::Transform offer some ways to specify non-standard orientations of coordinate system, seems that invert angle is only way to get counterclockwise rotation (Small notice: in term inverse angle I mean negative angle, non angle + 180).

Problem solved, I hope, thank you for your attention.

6
Graphics / Re: Rotate points via Transform
« on: January 09, 2013, 04:26:57 pm »
I think that full code in my case is no matter, but if you want: http://www.mediafire.com/?giz1xtn8626w2zj
Here are all need sources and the file witch contain points of outline and origin point of this.

Finally I'm find problem in rotation: it was rounding problem.

Now I have another problem: how to rotate point counterclockwise via sf::Transform, without inverting an angle ?

Look (green figure is original, blue figure is rotated on 45 degrees):

Good (via transform matrix): http://s2.ipicture.ru/uploads/20130109/jgY6v3Qb.jpg
Bad (via sf::Transform): http://s2.ipicture.ru/uploads/20130109/GN3NiStT.jpg

Good code:

for (std::vector<sf::Vector2i>::iterator cell = m_Current.begin(); cell != m_Current.end(); cell++) {
        cell->x -= getOrigin().x;
        cell->y -= getOrigin().y;
}

const double sin_val = std::sin(Castings::toRadians(getRotation()));
const double cos_val = std::cos(Castings::toRadians(getRotation()));

for (std::vector<sf::Vector2i>::iterator cell = m_Current.begin(); cell != m_Current.end(); cell++) {
        int new_x = static_cast<int>(std::floor((cell->x*cos_val + cell->y*sin_val) + 0.5));
        int new_y = static_cast<int>(std::floor((cell->y*cos_val - cell->x*sin_val) + 0.5));
        cell->x = new_x;
        cell->y = new_y;
}

Bad code:

sf::Transform matrix;

matrix.translate(getOrigin().x, getOrigin().y).rotate(getRotation());

for (std::vector<sf::Vector2i>::iterator cell = m_Current.begin(); cell != m_Current.end(); cell++) {
        int new_x = static_cast<int>(floor(matrix.transformPoint(cell->x, cell->y).x + 0.5));
        int new_y = static_cast<int>(floor(matrix.transformPoint(cell->x, cell->y).y + 0.5));
        cell->x = new_x;
        cell->y = new_y;
}

7
Graphics / Re: Rotate points via Transform
« on: January 09, 2013, 04:21:14 am »
I'm find one of the problems, but result still wrong:

for (std::vector<sf::Vector2i>::iterator cell = m_Current.begin(); cell != m_Current.end(); cell++) {
        cell->x = static_cast<int>(matrix.transformPoint(cell->x, cell->y).x + 0.5); // here cell->x modified
        cell->y = static_cast<int>(matrix.transformPoint(cell->x, cell->y).y + 0.5); // ... and used again
}

 
Also I'm find some solution on the StackOverflow, and wrote update without sf::Trasform.

http://stackoverflow.com/questions/7440900/function-to-rotate-a-point-around-another-point

Weeve thanks for the code, it's interesting, but can you give full code, because some functions are not defined here (I bad know English (you see) and English math terms, therefore it hard to me to understand it.)

8
Graphics / Rotate points via Transform
« on: January 08, 2013, 10:18:12 am »
I'm make class, witch handle collision map for object. After changing origin, position or angle the update function invoke. This function should make the returnable vector (m_Current) with points in their positions on the global collision map. When I trying to rotate line ([0,0] [0,1] [0,2] [0,3]) at 45 degrees, it return wrong result.

getOrigin() and getPosition() return sf::Vector2u
getRotation() return int

void CollisionOutline::update() {

        m_Current = m_Original;

        sf::Transform matrix;

        matrix.translate(-(getOrigin().x), -(getOrigin().y)).rotate(getRotation());

        for (std::vector<sf::Vector2i>::iterator cell = m_Current.begin(); cell != m_Current.end(); cell++) {
                cell->x = static_cast<int>(matrix.transformPoint(cell->x, cell->y).x + 0.5);
                cell->y = static_cast<int>(matrix.transformPoint(cell->x, cell->y).y + 0.5);
        }

        for (std::vector<sf::Vector2i>::iterator cell = m_Current.begin(); cell != m_Current.end(); cell++) {
                cell->x += getPosition().x;
                cell->y += getPosition().y;
        }

}

Pages: [1]