Get Image Position

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

Get Image Position

#1 Post by Kaen »

Is it possible to store the position of a image while it's moving?

Something like this:

Code: Select all

transform move_ball:
    xpos 0.5 ypos 0.15
    linear 2.0 xalign 0.0 ypos 0.1

image ball = "ball.png"
show ball at move_ball

python:
    while True:
        x = ball.xpos
        y = ball.ypos
        renpy.log(x+","+y)  

User avatar
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

Re: Get Image Position

#2 Post by Kaen »

Bump. Please help!

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Get Image Position

#3 Post by xela »

I am not sure how to get to the displayable properties if they were called with show, maybe there is some function or lookup in dict. In any case I never bother to find out cause Ren'Py script is best used for VN stuff and python for messing with transforms.

Code: Select all

transform move_ball:
    xpos 0.5 ypos 0.15
    linear 2.0 xalign 0.0 ypos 0.1

image ball = "ball.png"
screen ball(ball):
    text "[ball.xpos], [ball.ypos]"
    timer 0.1 action renpy.restart_interaction repeat True

label start:
    $ ball = At(ImageReference("ball"), move_ball)
    show expression ball
    call screen ball(ball)
Like what we're doing? Support us at:
Image

User avatar
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

Re: Get Image Position

#4 Post by Kaen »

Your code was very helpful, thank you xela! I changed it a bit and got some questions.

Code: Select all

screen ball(ball):
    text "[ball.xpos], [ball.ypos]"
    default xy = "%d,%d" % (ball.xpos, ball.ypos)
    timer 0.1 action (SetScreenVariable("xy", xy + " _ %d,%d" % (ball.xpos, ball.ypos)), renpy.restart_interaction) repeat True
    timer 2.0 action Return(xy)

label start:
    $ ball = At(ImageReference("ball"), move_ball)
    show expression ball
    call screen ball(ball)
    $ value = _return
    $ renpy.log(value)
    return
• I wanna store the positions on a variable but it seems I'm not getting the updated positions. Any idea on how to fix this? On the log file I always get tons of "0,0 _ 0,0 _..."

• The positions displayed on the ball screen are on the form of values from 0 to 1. There is a way to get those values as pixels?

• I wanna the pixel values so I can use them on the update function for the rhythm game. That function can work with values from 0 to 1 form?

Code: Select all

def update(self):
            if store.t + self.delay < time.time():
                self.moving = True
                #self.x = self.x + self.speed
            else:
                pass

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Get Image Position

#5 Post by xela »

Kaen wrote:Your code was very helpful, thank you xela! I changed it a bit and got some questions.

Code: Select all

screen ball(ball):
    text "[ball.xpos], [ball.ypos]"
    default xy = "%d,%d" % (ball.xpos, ball.ypos)
    timer 0.1 action (SetScreenVariable("xy", xy + " _ %d,%d" % (ball.xpos, ball.ypos)), renpy.restart_interaction) repeat True
    timer 2.0 action Return(xy)

label start:
    $ ball = At(ImageReference("ball"), move_ball)
    show expression ball
    call screen ball(ball)
    $ value = _return
    $ renpy.log(value)
    return
• I wanna store the positions on a variable but it seems I'm not getting the updated positions. Any idea on how to fix this? On the log file I always get tons of "0,0 _ 0,0 _..."
You can try this, screen will act as a timer reporting the values. It should work:

Code: Select all

screen ball(ball):
    text "[ball.xpos], [ball.ypos]"
    timer 0.1 action (Function(renpy.log, "%d,%d" % (ball.xpos, ball.ypos)), renpy.restart_interaction) repeat True

label start:
    $ ball = At(ImageReference("ball"), move_ball)
    show expression ball
    call screen ball(ball)
Kaen wrote:• The positions displayed on the ball screen are on the form of values from 0 to 1. There is a way to get those values as pixels?
[/code]
Take the width/height of the screen and multiply with the values accordingly. If you need them to report in that way, you need to define them like that in the transform (pos(100, 100) instead of pos(0.1, 0.1) for a 1000 by 1000 resolution for example). If there is another way, I am not aware of it without digging in code/docs.
Kaen wrote:• I wanna the pixel values so I can use them on the update function for the rhythm game. That function can work with values from 0 to 1 form?
I am not sure, I never use positions as floats. You can always try :)

==========================================================
I assume that you understood the code in the last post well enough to provide positions and speed to the sprites so why are you doing this?
Like what we're doing? Support us at:
Image

User avatar
Kaen
Regular
Posts: 148
Joined: Tue Oct 16, 2012 10:49 pm
Contact:

Re: Get Image Position

#6 Post by Kaen »

xela wrote:You can try this, screen will act as a timer reporting the values. It should work:
Oh, so that's how we do it on the smart way haha. I understand but it seems the code is behaving just like mine. On the log we just get "0,0" recorded, the positions are not being updated.
xela wrote:I assume that you understood the code in the last post well enough to provide positions and speed to the sprites so why are you doing this?
Yup, I could understand that! The real problem is that I can't simply make xpos += speed or ypos +=1 on that function because the movement I need the sprite to do is not like this. I was trying to figure out the xy coordinates of that movement so I could apply them into the function >.<

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Get Image Position

#7 Post by xela »

Kaen wrote: Oh, so that's how we do it on the smart way haha. I understand but it seems the code is behaving just like mine. On the log we just get "0,0" recorded, the positions are not being updated.
It will do it's thing when you switch to integer positioning. You format the values as %d, that's why positioning floats are coming out as 0.
Kaen wrote: Yup, I could understand that! The real problem is that I can't simply make xpos += speed or ypos +=1 on that function because the movement I need the sprite to do is not like this. I was trying to figure out the xy coordinates of that movement so I could apply them into the function >.<
I think the idea is to provide sets of coordinates and speed and move the displayable using those values. I doubt you'll ever get it done right by re-rendering the displayable on new positions all the time. At the very least, I don't think getting values every 0.1 second will be enough to define a clear path without jumpy images, but I've never really tried.

But if you're sticking with your approach, why not save to dict? You can ship the dict with the game and you will not have to do anything by hand.
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: piinkpuddiin