I thank all those who responded to my question

Here is the code that I had written.

When updating is applied to a surface object the rotation works but when it
is applied through a class to an object it goes wrong in about 3 rotations.
As far as I can see the code is the same. What is wrong? If you can correct
some code and show me would help.

On Tue, Apr 28, 2015 at 11:58 PM, Oscar Benjamin <oscar.j.benja...@gmail.com
> wrote:

> On 28 April 2015 at 16:38, diliup gabadamudalige <dili...@gmail.com>
> wrote:
> >
> > Looking at the code on this page lines 47 & 48
> >
> >
> http://programarcadegames.com/python_examples/show_file.php?file=sprite_circle_movement.py
> >
> > is there a way to do
> > self.rect.x +*= some value*
> > self.rect.y += some value
> >
> > rather than
> >
> > self.rect.x = self.radius * math.sin(self.angle) + self.center_x
> > self.rect.y = self.radius * math.cos(self.angle) + self.center_y
>
> There's no way to do that. The second version ignores the previous
> value of self.rect.x/y.
>
> I'm going to guess that the angle has only changed by a small amount
> delta_angle between iterations in which case there would be a way to
> do it approximately.
>
> The real question is just why though? The second version is correct
> and will be correct for ever. The first version would only be
> approximately correct and over time you'd probably find that the
> position would drift so that the ball was effectively at a different
> radius.
>
> In any case if
>      x = r * sin(theta)
> then
>     dx/dt = r * cos(theta) * dtheta/dt.
> Since
>     y = r * cos(theta) that's
>     dy/dt = x * dtheta/dt.
> So you could update y approximately with:
>     y += x * dtheta/dt * deltat
> where deltat is your timestep. In your code that would be:
>     self.rect.x += self.rect.y * self.speed
>     self.rect.y -= self.rect.x * self.speed
>
> Really what you have now is better though.
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
import sys, os, pygame, itertools
from math import sin,cos,pi, radians
from pygame.locals import *

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50,50) #Set window position

pygame.init()
clock = pygame.time.Clock()
FPS = 1000

SCREENW = 800   #screen width
SCREENH = 740   #screen height

BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
ORANGE = (128, 100, 30)
FONT1= "Cookie-Regular.ttf"

SCREEN = pygame.display.set_mode((SCREENW, SCREENH), 0, 32) #display screen
clock = pygame.time.Clock()

#-------------------------------------------------------------------------------
def maketext(msg,fontsize, colour = ORANGE, font = FONT1):
    mafont = pygame.font.Font(font, fontsize)
    matext = mafont.render(msg, True, colour)
    matext = matext.convert_alpha()
    return matext

#-------------------------------------------------------------------------------
def print_info():
    """"""
    textcos = maketext(str(round(obj.rect.x, 2)) + "   " + 
str(round(obj.rect.y, 2)), 30)
    SCREEN.blit(textcos, (obj.rect.x, obj.rect.y + 30))

#-------------------------------------------------------------------------------
class object_factory(pygame.sprite.Sprite):

    def __init__(self, imagelist, xpos, ypos, speedx = 0, speedy = 0, value = 
0):
        """Constructor"""
        pygame.sprite.Sprite.__init__(self)
        self.name = ""
        self.frame = 0
        self.imagelist = imagelist
        self.image = imagelist[self.frame]
        self.mask = pygame.mask.from_surface(self.image) # pixelmask
        self.rect = self.image.get_rect()
        self.rect.x = xpos
        self.rect.y = ypos
        #self.speedx = speedx
        #self.speedy = speedy
        self.timer = 0
        self.timerlimit = 10

        #----------------------------------------------------------------------
    #def move(self):  # wallsprites, Herosprite, looptime
        #self.rect.x += self.speedx
        #self.rect.y += self.speedy

    #----------------------------------------------------------------------
    def update(self):
        """"""
        self.image = self.imagelist[self.frame]
        if self.timer >= self.timerlimit:
            self.frame += 1
            if self.frame >= len(self.imagelist):
                self.frame = 0
            self.timer = 0
        self.timer += 1

plat = pygame.image.load("plt0.png").convert_alpha()
star = pygame.image.load("gemp0.png").convert_alpha()
#box = pygame.image.load("crateB.png").convert_alpha()

platforms = pygame.sprite.Group()
boxes = pygame.sprite.Group()

rotcenx = SCREENW/2
rotceny = SCREENH/2

radius = 200
angle = radians(90)  #pi/4 # starting angle 45 degrees
omega = radians(5) #Angular velocity

m = rotcenx + radius * cos(angle) #Starting position x
n = rotceny - radius * sin(angle) #Starting position y

for _ in itertools.repeat(None, 1):
    madyax = SCREENW/2
    madyay = SCREENH/2

    araya = 200
    konaya = radians(180)  #pi/4 # starting angle 45 degrees
    konika_pravegaya = radians(5) #Angular velocity

    a = madyax + (araya * cos(konaya)) #Starting position x
    b = madyay - (araya * sin(konaya)) #Startinh position y

    plat = object_factory([plat], a, b)
    plat.araya = araya
    plat.konaya = konaya
    plat.kp = konika_pravegaya


    platforms.add(plat)

while True:
    ms = clock.tick(FPS)  # milliseconds passed since last frame
    #looptime = milliseconds / 1000.0 # seconds passed since last frame

    SCREEN.fill((BLACK))

    pygame.draw.circle(SCREEN, BLUE, (SCREENW / 2, SCREENH / 2), 5)

    ##-----------------------------------------------------------
    SCREEN.blit(star, (m, n)) # Draw current x,y

    angle = angle + omega # New angle, we add angular velocity
    m = m + radius * omega * cos(angle + pi / 2) # New x
    n = n - radius * omega * sin(angle + pi / 2) # New y
    ##-----------------------------------------------------------
    # show object anchored to center of rotation
    pygame.draw.line(SCREEN, ORANGE, (rotcenx, rotceny), (m, n))

    text = maketext(str(radius), 30)
    SCREEN.blit(text, (m, n - 40))

    text = maketext((str(round(m, 2)) + "  " + str(round(n, 2))), 30)
    SCREEN.blit(text, (m, n + 40)) # Draw current x,y

    ##------------------------------------------------------------------
    for plat in platforms:

        plat.konaya = plat.konaya + plat.kp

        plat.rect.x = plat.rect.x + plat.araya * plat.kp * cos(plat.konaya + pi 
/ 2)
        plat.rect.y = plat.rect.y - plat.araya * plat.kp * sin(plat.konaya + pi 
/ 2)
    ##------------------------------------------------------------------------
    pygame.draw.line(SCREEN, ORANGE, (madyax, madyay), (plat.rect.x, 
plat.rect.y))

    platforms.update()
    platforms.draw(SCREEN)

    pygame.event.pump()
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == 
K_ESCAPE):
            pygame.quit()
            sys.exit()

    pygame.display.update()
    pygame.time.wait(100)
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to