Hi folks. I'm trying to get an object to move toward a target.
Sometimes it works, other times it gets 'stuck' and jitters back and
forth between two adjacent points, never reaching the target.

Here's some code that should run find. Just click the mouse button to
lay down a target. Click again to lay down another target. The object
will move to each target in sequence -- though it will likely get
stuck.

Would appreciate any help or suggestions. Cheers.

===

import pyglet, random
from math import radians, degrees, atan2, sin, cos, sqrt
from pyglet import window, clock, font, gl

class MainWindow(window.Window):
    def __init__(self, *args, **kwargs):
        window.Window.__init__(self, *args, **kwargs)
        self.create_player()
        self.targets = []
        self.mouse_pos = (0, 0)
        self.mouse_angle = 0

    def create_player(self):
        self.player = PlayerObject(x=50, y=50, width=11, height=11,
speed=1)

    def mouse_click(self, x, y):
        self.targets.append(GameObject(x=x, y=y, width=16, height=16))
        self.player.path.append((x,y))

    def caption(self):
        current_pos = (self.player.x, self.player.y)
        current_angle = self.player.angle
        if len(self.player.path) == 0:
            target_pos = current_pos
        else:
            target_pos = (self.player.path[0][0], self.player.path[0]
[1])
        self.set_caption('Pos: %d, %d | Angle: %d | Target: %d, %d ||
Mouse: %d, %d | Angle: %d' %
                (current_pos[0], current_pos[1], current_angle,
                    target_pos[0], target_pos[1],
                    self.mouse_pos[0], self.mouse_pos[1],
self.mouse_angle))

    def on_mouse_press(self, x, y, button, modifiers):
        main_window.mouse_click(x,y)

    def on_mouse_motion(self, x, y, dx, dy):
        self.mouse_pos = (x, y)
        self.get_mouse_angle()

    def get_mouse_angle(self):
        origin = (400, 100)
        self.mouse_angle = degrees(atan2(self.mouse_pos[1] -
origin[1], self.mouse_pos[0] - origin[0]))
        if self.mouse_angle < 0: self.mouse_angle += 360

    def on_draw(self):
        self.clear()
        if len(self.targets) > 0:
            for o in self.targets:
                o.draw()
        self.player.draw()

    def update(self, dt):
        self.player.move()
        self.caption()

class GameObject(object):

    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
        (self.x_edge, self.y_edge) = ((self.width / 2), (self.height /
2))

    def draw(self):
        pyglet.gl.glLoadIdentity()
        pyglet.gl.glTranslatef(self.x, self.y, 0)
        pyglet.gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
        pyglet.graphics.draw(4, pyglet.gl.GL_QUADS,
                ('v2i', ((0 + self.x_edge), (0 + self.y_edge),
                    (0 - self.x_edge), (0 + self.y_edge),
                    (0 - self.x_edge), (0 - self.y_edge),
                    (0 + self.x_edge), (0 - self.y_edge)))
                    )

class PlayerObject(GameObject):
    def __init__(self, *args, **kwargs):
        GameObject.__init__(self, *args, **kwargs)
        self.path = [(self.x, self.y)]
        self.angle = 0

    def move(self):
        if len(self.path) > 0:
            target = self.path[0]
            self.angle = degrees(atan2(self.y - target[1], self.x -
target[0]))
            if self.angle < 0: self.angle += 360
            next_xy = self.get_next_xy(target)
            (self.x, self.y) = (next_xy[0], next_xy[1])

    def get_next_xy(self, target):
        if (self.iterations_till_checkpoint(target) < 1.0):
            self.path.pop(0)
            return target
        else:
            next_x = self.x + (cos(self.angle) * self.speed)
            next_y = self.y + (sin(self.angle) * self.speed)
            return (next_x, next_y)

    def iterations_till_checkpoint(self, target):
        (dy, dx) = (self.y - target[1], self.x - target[0])
        distance = sqrt(dx**2 + dy**2)
        return distance / self.speed

if __name__ == '__main__':
    main_window = MainWindow(800, 200)
    pyglet.clock.schedule_interval(main_window.update, 1/60.0)
    pyglet.app.run()

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en.

Reply via email to