Converting joystick axis values to hex triplet codes - python

Using a PS4 controller in pygame, I already figured out how to capture axis rotation which can vary to a -1 or 1, but I don't know how to convert those numbers to a color ring like scale in order to turn it into a hex triplet number.
The values mimicking that of a color ring is more important than anything as I don't want the joystick capturing a color while it's not in motion. Picture
( Since that was a bit confusing, essentially I want to be able to move my joystick around and capture an accurate hex triplet number based on where it has moved )
This is my code so far:
import pygame
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 62, 210, 255)
# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 25)
def print(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def reset(self):
self.x = 25
self.y = 25
self.line_height = 30
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
# Set the width and height of the screen [width,height]
size = [900, 1080]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("PS4Testing")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
textPrint = TextPrint()
# -------- Main Program Loop -----------
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks
joystick_count = pygame.joystick.get_count()
# For each joystick:
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
for i in range( axes ):
axis = joystick.get_axis( i )
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
textPrint.unindent()
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()
Modified code from official pygame documentation
Any help would be greatly appreciated

My plan:
Find the angle of stick on joystick
Get RGB value using HSV and stick's angle
Convert to HEX
Finding the angle of joystick's stick
First, we need to find the joystick's angle. We can do this using the law of cosines and axis statements as lengths of the sides of a triangle (because they are from one point/center).
Store axis statements in this block:
for i in range( axes ):
axis = joystick.get_axis( i )
#Storing axis statement
if i == 0:
Xaxis = axis
elif i == 1:
Yaxis = axis
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
We store them because in for loop we can take only one statement at a time.
Then define a function that will return the angle of the stick. We need to use math module for pi and acos.
def get_angle(Xaxis,Yaxis):
#To avoid ZeroDivisionError
#P.S. - you can improve it a bit.
if Xaxis == 0:
Xaxis = 0.001
if Yaxis == 0:
Yaxis = 0.001
#defining the third side of a triangle using the Pythagorean theorem
b = ((Xaxis)**2 + (Yaxis)**2)**0.5
c = Xaxis
a = Yaxis
#Using law of cosines we'll find angle using arccos of cos
#math.acos returns angles in radians, so we need to multiply it by 180/math.pi
angle = math.acos((b**2 + c**2 - a**2) / (2*b*c)) * 180/math.pi
#It'll fix angles to be in range of 0 to 360
if Yaxis > 0:
angle = 360 - angle
return angle
Now get_angle(Xaxis,Yaxis) will return stick's angle. East is 0°, north is 90°, west is 180°, south is 270°.
Getting HSV and converting to RGB
We have stick's angle and we can use it to make an HSV color and then convert it to RGB, because Color Wheel is based on hue of the colors.
Hue has the same range as the stick's angle, so we can use stick's angle as hue.
Using the formulas from Wikipedia, define a function that converts HSV to RGB.
def hsv_to_rgb(H,S,V):
#0 <= H <= 360
#0 <= S <= 1
#0 <= V <= 1
C = V * S
h = H/60
X = C * (1 - abs(h % 2 -1))
#Yes, Python can compare like "0 <= 2 > 1"
if 0 <= h <= 1:
r = C; g = X; b = 0
elif 1 <= h <= 2:
r = X; g = C; b = 0
elif 2 <= h <= 3:
r = 0; g = C; b = X
elif 3 <= h <= 4:
r = 0; g = X; b = C
elif 4 <= h <= 5:
r = X; g = 0; b = C
elif 5 <= h < 6:
r = C; g = 0; b = X
m = V - C
#Final computing and converting from 0 - 1 to 0 - 255
R = int((r+m)*255)
G = int((g+m)*255)
B = int((b+m)*255)
return [R,G,B]
Now hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1) will return list of red, green and blue in range of 0 - 255 (for an easier conversion in hex). Saturation and Value are not necessary in this case.
Conversion to HEX
The easiest part. We need to get the list of RGB and convert values to hex.
colors = hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Converting to hex
lst = list(map(hex,colors))
#Cutting the "0x" part
for i in range(len(lst)):
lst[i] = lst[i][2:]
#If one of the colors has only one digit, extra 0 will be added for a better look
if len(lst[i]) == 1:
lst[i] = "0"+str(lst[i])
print(get_angle(Xaxis,Yaxis))
Something like #ff0000, #00ff00, and #0000ff will be printed.
You also can make your program to show color change in real time, simply add WHITE = colors in this block. DO NOT use it if you have photosensitive epilepsy.
Merging everything
Place both functions from first and second points somewhere at the beginning.
Add the storing from the first point to the block
for i in range( axes ):
axis = joystick.get_axis( i )
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
Add the conversion from the third point after the block. I recommend to make a death zone feature.
death_zone = 0.1
if abs(Xaxis) > death_zone or abs(Yaxis) > death_zone:
#If you prefer HSV color wheel, use hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Else if you prefer RGB color wheel, use hsv_to_rgb(360-get_angle(Xaxis,Yaxis),1,1)
colors = hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Converting to hex
lst = list(map(hex,colors))
#Cutting the "0x" part
for i in range(len(lst)):
lst[i] = lst[i][2:]
#If one of the colors has only one digit, extra 0 will be added for a better look
if len(lst[i]) == 1:
lst[i] = "0"+str(lst[i])
print("#"+"".join(lst))
That's how your code may look after all:
P.S. - you will probably have to change some code, because I thing my joystick's axis weren't properly captured.
import pygame
import math
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 62, 210, 255)
def hsv_to_rgb(H,S,V):
#Accirding to https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
#0 <= H <= 360
#0 <= S <= 1
#0 <= V <= 1
C = V * S
h = H/60
X = C * (1 - abs(h % 2 -1))
#Yes, Python can compare like "0 <= 2 > 1"
if 0 <= h <= 1:
r = C; g = X; b = 0
elif 1 <= h <= 2:
r = X; g = C; b = 0
elif 2 <= h <= 3:
r = 0; g = C; b = X
elif 3 <= h <= 4:
r = 0; g = X; b = C
elif 4 <= h <= 5:
r = X; g = 0; b = C
elif 5 <= h < 6:
r = C; g = 0; b = X
m = V - C
#Final computing and converting from 0 - 1 to 0 - 255
R = int((r+m)*255)
G = int((g+m)*255)
B = int((b+m)*255)
return [R,G,B]
def get_angle(Xaxis,Yaxis):
#To avoid ZeroDivisionError
#P.S. - you can improve it a bit.
if Xaxis == 0:
Xaxis = 0.001
if Yaxis == 0:
Yaxis = 0.001
#defining the third side of a triangle using the Pythagorean theorem
b = ((Xaxis)**2 + (Yaxis)**2)**0.5
c = Xaxis
a = Yaxis
#Using law of cosines we'll fing angle using arccos of cos
#math.acos returns angles in radians, so we need to multiply it by 180/math.pi
angle = math.acos((b**2 + c**2 - a**2) / (2*b*c)) * 180/math.pi
#It'll fix angles to be in range of 0 to 360
if Yaxis > 0:
angle = 360 - angle
return angle
# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 25)
def print(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def reset(self):
self.x = 25
self.y = 25
self.line_height = 30
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
# Set the width and height of the screen [width,height]
size = [900, 1080]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("PS4Testing")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
textPrint = TextPrint()
# -------- Main Program Loop -----------
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks
joystick_count = pygame.joystick.get_count()
# For each joystick:
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
for i in range( axes ):
axis = joystick.get_axis( i )
#Storing axis statement
if i == 0:
Xaxis = axis
elif i == 1:
Yaxis = axis
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
textPrint.unindent()
#If joystick is not in the center
#Death zone is used to not capture joystick if it's very close to the center
death_zone = 0.1
if abs(Xaxis) > death_zone or abs(Yaxis) > death_zone:
#If you prefer HSV color wheel, use hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Else if you prefer RGB color wheel, use hsv_to_rgb(360-get_angle(Xaxis,Yaxis),1,1)
colors = hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Converting to hex
lst = list(map(hex,colors))
#Cutting the "0x" part
for i in range(len(lst)):
lst[i] = lst[i][2:]
#If one of the colors has only one digit, extra 0 will be added for a better look
if len(lst[i]) == 1:
lst[i] = "0"+str(lst[i])
print("#"+"".join(lst))
#You can use it to see color change in real time.
#But I don't recomend to use it if you have photosensitive epilepsy.
#WHITE = colors
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()

Related

Problem with animating a sprite in pygame

i have a problem with this code, i am a new person with programming and been using the book "how to think like a computer scientist 3rd edition" and he did not solve exercise 2 of chapter 17 this given: "he deliberately left a mistake in the code to animate Duke. If you click on one of the checkerboard squares to the right of Duke, he salutes anyway. Why? Find a one-line solution to the error ", I've tried many forms but I have not succeeded, I leave you all the code and the images that I have used
PS: images must have the name: ball.png and duke_spritesheet.png
import pygame
gravity = 0.025
my_clock = pygame.time.Clock()
class QueenSprite:
def __init__(self, img, target_posn):
self.image = img
self.target_posn = target_posn
(x, y) = target_posn
self.posn = (x, 0) # Start ball at top of its column
self.y_velocity = 0 # with zero initial velocity
def update(self):
self.y_velocity += gravity
(x, y) = self.posn
new_y_pos = y + self.y_velocity
(target_x, target_y) = self.target_posn # Unpack the position
dist_to_go = target_y - new_y_pos # How far to our floor?
if dist_to_go < 0: # Are we under floor?
self.y_velocity = -0.65 * self.y_velocity # Bounce
new_y_pos = target_y + dist_to_go # Move back above floor
self.posn = (x, new_y_pos) # Set our new position.
def draw(self, target_surface): # Same as before.
target_surface.blit(self.image, self.posn)
def contains_point(self, pt):
""" Return True if my sprite rectangle contains point pt """
(my_x, my_y) = self.posn
my_width = self.image.get_width()
my_height = self.image.get_height()
(x, y) = pt
return ( x >= my_x and x < my_x + my_width and
y >= my_y and y < my_y + my_height)
def handle_click(self):
self.y_velocity += -2 # Kick it up
class DukeSprite:
def __init__(self, img, target_posn):
self.image = img
self.posn = target_posn
self.anim_frame_count = 0
self.curr_patch_num = 0
def update(self):
if self.anim_frame_count > 0:
self.anim_frame_count = (self.anim_frame_count + 1 ) % 60
self.curr_patch_num = self.anim_frame_count // 6
def draw(self, target_surface):
patch_rect = (self.curr_patch_num * 50, 0,
50, self.image.get_width())
target_surface.blit(self.image, self.posn, patch_rect)
def contains_point(self, pt):
""" Return True if my sprite rectangle contains pt """
(my_x, my_y) = self.posn
my_width = self.image.get_width()
my_height = self.image.get_height()
(x, y) = pt
return ( x >= my_x and x < my_x + my_width and
y >= my_y and y < my_y + my_height)
def handle_click(self):
if self.anim_frame_count == 0:
self.anim_frame_count = 5
def draw_board(the_board):
""" Draw a chess board with queens, as determined by the the_board. """
pygame.init()
colors = [(255,0,0), (0,0,0)] # Set up colors [red, black]
n = len(the_board) # This is an NxN chess board.
surface_sz = 480 # Proposed physical surface size.
sq_sz = surface_sz // n # sq_sz is length of a square.
surface_sz = n * sq_sz # Adjust to exactly fit n squares.
# Create the surface of (width, height), and its window.
surface = pygame.display.set_mode((surface_sz, surface_sz))
ball = pygame.image.load("ball.png")
# Use an extra offset to centre the ball in its square.
# If the square is too small, offset becomes negative,
# but it will still be centered :-)
ball_offset = (sq_sz-ball.get_width()) // 2
all_sprites = [] # Keep a list of all sprites in the game
# Create a sprite object for each queen, and populate our list.
for (col, row) in enumerate(the_board):
a_queen = QueenSprite(ball,
(col*sq_sz+ball_offset, row*sq_sz+ball_offset))
all_sprites.append(a_queen)
# Load the sprite sheet
duke_sprite_sheet = pygame.image.load("duke_spritesheet.png")
# Instantiate two duke instances, put them on the chessboard
duke1 = DukeSprite(duke_sprite_sheet,(sq_sz*2, 0))
duke2 = DukeSprite(duke_sprite_sheet,(sq_sz*5, sq_sz))
# Add them to the list of sprites which our game loop manages
all_sprites.append(duke1)
all_sprites.append(duke2)
while True:
# Look for an event from keyboard, mouse, etc.
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
break;
if ev.type == pygame.KEYDOWN:
key = ev.dict["key"]
if key == 27: # On Escape key ...
break # leave the game loop.
if key == ord("r"):
colors[0] = (255, 0, 0) # Change to red + black.
elif key == ord("g"):
colors[0] = (0, 255, 0) # Change to green + black.
elif key == ord("b"):
colors[0] = (0, 0, 255) # Change to blue + black.
if ev.type == pygame.MOUSEBUTTONDOWN: # Mouse gone down?
posn_of_click = ev.dict["pos"] # Get the coordinates.
for sprite in all_sprites:
if sprite.contains_point(posn_of_click):
sprite.handle_click()
break
for sprite in all_sprites:
sprite.update()
# Draw a fresh background (a blank chess board)
for row in range(n): # Draw each row of the board.
c_indx = row % 2 # Alternate starting color
for col in range(n): # Run through cols drawing squares
the_square = (col*sq_sz, row*sq_sz, sq_sz, sq_sz)
surface.fill(colors[c_indx], the_square)
# Now flip the color index for the next square
c_indx = (c_indx + 1) % 2
# Ask every sprite to draw itself.
for sprite in all_sprites:
sprite.draw(surface)
my_clock.tick(60) # Waste time so that frame rate becomes 60 fps
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
draw_board([0, 5, 3, 1, 6, 4, 2]) # 7 x 7 to test window size
PS: I think the error is here but it did not succeed
return ( x >= my_x and x + my_width and y >= my_y and y < my_y + my_height)
The issue is caused by the face, that "duke_spritesheet.png" is a sprite sheet. When you define the rectangular region which is covered by the object, then you have to use the width of a single image, rather than the width of the entire sprite sheet:
my_width = self.image.get_width()
my_width = 50
Change this in the method contains_point of the class DukeSprite:
class DukeSprite:
# [...]
def contains_point(self, pt):
""" Return True if my sprite rectangle contains pt """
(my_x, my_y) = self.posn
my_width = 50
my_height = self.image.get_height()
(x, y) = pt
return ( x >= my_x and x < my_x + my_width and
y >= my_y and y < my_y + my_height)

A-star algorithm results in weird, slow and inefficient choices

Today I attempted making the A-STAR pathfinding algorithm, with visualization in PyGame. The code is:
import pygame, time
pygame.init()
pygame.font.init()
#----------[0,1,2]
#GRID[x][y][g/h/f]
#g_cost: distance from starting to current node
#h_cost: distance from ending to current node
#f_cost: sum of previous two costs. That way, the lower
#the f cost, the more likely a node is to be on the path
WIDTH = 20
HEIGHT = 15
XSTART = 5
YSTART = 5
XEND = 15
YEND = 10
TICK = 10000000
CLOCK = pygame.time.Clock()
OPEN_NODES = []
CLOSED_NODES = []
BEACON = 0
SQUARE = 50
GEN = 0
WHITE = (255,255,255)
GRID = []
#func to find distance between 2 nodes
def find_dist(coor1,coor2):
x1,y1 = coor1
x2,y2 = coor2
x_diff = abs(x1-x2)
y_diff = abs(y1-y2)
smaller = min([x_diff,y_diff])
#diagonal multiplied by sqrt(2) approximately
dist = round(smaller*1.4+(max([x_diff,y_diff])-smaller))*10
return dist
#super stupid way to get all node neighbors
def get_neighbors(coors):
x,y = coors
neighbors = []
if x-1 > -1:
neighbors.append(GRID[x-1][y])
if x-1 > -1 and y+1 < HEIGHT:
neighbors.append(GRID[x-1][y+1])
if y+1 < HEIGHT:
neighbors.append(GRID[x][y+1])
if y+1 < HEIGHT and x+1 < WIDTH:
neighbors.append(GRID[x+1][y+1])
if x+1 < WIDTH:
neighbors.append(GRID[x+1][y])
if x+1 < WIDTH and y-1 > -1:
neighbors.append(GRID[x+1][y-1])
if y-1 > -1:
neighbors.append(GRID[x][y-1])
if y-1 > -1 and x-1 > -1:
neighbors.append(GRID[x-1][y-1])
return neighbors
#returns list without duplicates
def elim_duplicates(listin):
for element in listin:
if listin.count(element) > 1:
listin.pop(listin.index(element))
return listin
#creates main grid
for x in range(WIDTH):
COLUMN = []
for y in range(HEIGHT):
COLUMN.append([-1,-1])
GRID.append(COLUMN)
GRID[XSTART][YSTART] = [-2,-2]
GRID[XEND][YEND] = [-3,-3]
#adds costs to each node
for x in range(WIDTH):
for y in range(HEIGHT):
g_cost = find_dist([x,y],[XSTART,YSTART])
h_cost = find_dist([x,y],[XEND,YEND])
f_cost = g_cost + h_cost
GRID[x][y] = [g_cost,h_cost,f_cost,x,y]
#adds the starting node to open nodes,
#which is a list containing all nodes
#that will be checked for the lowest f cost
OPEN_NODES.append(GRID[XSTART][YSTART])
WINDOW = pygame.display.set_mode((WIDTH*SQUARE, HEIGHT*SQUARE))
pygame.display.set_caption('A* Algorithm')
RUN = True
PATH_NODES = []
while RUN:
GEN += 1
CLOCK.tick(TICK)
#cycle control
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
#updates costs
for x in range(WIDTH):
for y in range(HEIGHT):
g_cost = find_dist([x,y],[XSTART,YSTART])
h_cost = find_dist([x,y],[XEND,YEND])
f_cost = g_cost + h_cost
GRID[x][y] = [g_cost,h_cost,f_cost,x,y]
#creates a list with only f costs
f_cost_list = []
for i in OPEN_NODES:
f_cost_list.append(i[2])
#finds the node with the lowest f cost
CURRENT_NODE = OPEN_NODES[f_cost_list.index(min(f_cost_list))]
#adds all neighbors of the current node and removes duplicates
OPEN_NODES.extend(get_neighbors(CURRENT_NODE[3:]))
OPEN_NODES = elim_duplicates(OPEN_NODES)
#adds current node to possible path nodes
PATH_NODES.append(CURRENT_NODE)
#removes current node from open nodes
OPEN_NODES.remove(CURRENT_NODE)
#removes all possible duplicates from lists
PATH_NODES = elim_duplicates(PATH_NODES)
OPEN_NODES = elim_duplicates(OPEN_NODES)
#checks if path has been found
if CURRENT_NODE[3] == XEND and CURRENT_NODE[4] == YEND:
pygame.draw.rect(WINDOW,(0,255,255), (490, 490, SQUARE, SQUARE))
print('DONE')
time.sleep(3)
pygame.quit()
#draws all open nodes
for i in OPEN_NODES:
pygame.draw.rect(WINDOW,(0,0,255),(i[3]*SQUARE,i[4]*SQUARE,SQUARE,SQUARE))
#draws all possible path nodes
for i in PATH_NODES:
pygame.draw.rect(WINDOW,(255,0,0),(i[3]*SQUARE,i[4]*SQUARE,SQUARE,SQUARE))
#draws start and end nodes
pygame.draw.rect(WINDOW,(0,255,0), (XSTART*SQUARE, YSTART*SQUARE, SQUARE, SQUARE))
pygame.draw.rect(WINDOW,(0,255,0), (XEND*SQUARE,YEND*SQUARE,SQUARE,SQUARE))
#shows generation
font = pygame.font.Font(None, 100)
text = font.render(str(GEN), 1, WHITE)
WINDOW.blit(text, (10,10))
#updates screen
pygame.display.update()
WINDOW.fill((0, 0, 0))
pygame.quit()
Whenever I run the algorithm with the starting and ending coordinates as shown, this is the weird result that shows:
It doesn't yet show the exact path, but the errors in the algorithm are clearly visible: holes, weird edges etc.
What could be causing this?

Python: PSO clumping <s>and code working only when mouse moves</s>

I'm trying to make a simple PSO (particle-swarm optimization) program.
Below is my current code, and I've been tweaking the weights only to find the "optimization" not working.
import random as ran
import math
# import threading as thr
import pygame as gm
# import pycuda as cuda
# import matplotlib.pyplot as plt
p_max = 10 # Maximum number of particles
rand_max = 100 # Maximum random vector value
# history = 10 # Number of iterations to record as history (including most current)
width = 600
height = 600
func_dict = {
"sphere" : ( lambda x, y: ( (x-(width/2))**2 + (y-(height/2))**2 ) ),
"booth" : ( lambda x, y: (((x-(width/2)) + 2*(y-(height/2)) - 7) ** 2) + ((2*(x-(width/2)) + (y-(height/2)) - 5) ** 2) ),
"matyas" : ( lambda x, y: (0.26 * ((x-(width/2))**2 + (y-(height/2))**2) - 0.48*(x-(width/2))*(y-(height/2))) )
} # (x-(width/2)) and (y-(height/2)) to shift the Zero to the display center
func = "sphere" # Choose function from func_dict
# Weights (0<w<1)
wLocal = 0.4 # Explore weight
wGlobal = 0.8 # Exploit weight
wRandom = 0.02 # Weight of random vector
global_best = [None, None, None] # Initial blank
class particle: # particles
global global_best
def __init__(self):
global global_best
global width, height
global func_dict, func
self.vel_x = 0
self.vel_y = 0
self.pos_x = ran.randint(0, width)
self.pos_y = ran.randint(0, height)
self.pos_z = func_dict[func](self.pos_x, self.pos_y)
self.local_best = [self.pos_x, self.pos_y, self.pos_z]
if (global_best[0] == None) or (global_best[1] == None) or (global_best[2] == None): # Is 1st particle
global_best = self.local_best
def update(self): # Update vectors
global width, height
global rand_max
global wGlobal, wLocal, wRandom
global global_best
self.vel_x = (wGlobal * (global_best[0] - self.pos_x)) + (wLocal * (self.local_best[0] - self.pos_x)) + (wRandom * ran.randint(-rand_max, rand_max))
self.vel_y = (wGlobal * (global_best[1] - self.pos_y)) + (wLocal * (self.local_best[1] - self.pos_y)) + (wRandom * ran.randint(-rand_max, rand_max))
# self.pos_x = (self.pos_x + self.vel_x) % width
# self.pos_y = (self.pos_y + self.vel_y) % height
self.pos_x += self.vel_x
self.pos_y += self.vel_y
if self.pos_x < 0:
self.pos_x = 0
if self.pos_y < 0:
self.pos_y = 0
if self.pos_x > width:
self.pos_x = width
if self.pos_y > height:
self.pos_y = height
self.pos_z = func_dict[func](self.pos_x, self.pos_y)
if self.pos_z < global_best[2]:
global_best = [self.pos_x, self.pos_y, self.pos_z]
particles = [None for _ in range(p_max)]
def initialize():
global_best = [None, None, None]
for foo in range(p_max):
particles[foo] = particle() # create new particles
# def dist(p1, p2): # distance
# return(math.sqrt( ( (p1.pos_x - p2.pos_y)**2) + ((p1.pos_y - p2.pos_y)**2) ) )
# def update(this): # this = particle
# this.vel_x = (wGlobal * (global_best[0] - this.pos_x)) + (wLocal * (this.local_best[0] - this.pos_x)) + (wRandom * ran.randint(0, rand_max))
# this.vel_y = (wGlobal * (global_best[1] - this.pos_y)) + (wLocal * (this.local_best[1] - this.pos_y)) + (wRandom * ran.randint(0, rand_max))
# # this.pos_x = (this.pos_x + this.vel_x) % width
# # this.pos_y = (this.pos_y + this.vel_y) % height
# this.pos_x += this.vel_x
# this.pos_y += this.vel_y
# if this.pos_x < 0:
# this.pos_x = 0
# if this.pos_y < 0:
# this.pos_y = 0
# if this.pos_x > width:
# this.pos_x = width
# if this.pos_y > height:
# this.pos_y = height
# # return this
# def update_multi(things): # things = list() of particles
# these = things
# for item in these:
# item = update(item)
# return these
gm.init()
main = gm.display.set_mode((width, height))
end_program = False
initialize()
main.fill((255, 255, 255))
while end_program == False:
# main.fill((255, 255, 255)) #Comment/Uncomment to leave trace
# plt.plot() # Plot functions
for event in gm.event.get():
if event.type == gm.QUIT:
end_program = True
for foo in range(len(particles)):
particles[foo].update()
gm.draw.circle(main, (0, 0, 0), (int(particles[foo].pos_x), int(particles[foo].pos_y)), 5, 0)
gm.display.flip()
Problem 1: Program only runs when mouse is moving
I'm not sure why but the program only runs fairly quickly for a few iterations but seems to just stop afterwards, but continues when the mouse is moved.
Problem 2: Particles appear to stay local
As I move the mouse around, it kinda runs. What emerges is clumps of traces left when the 2nd # main.fill((255, 255, 255)) is uncommented. The 1st initial traces from before the program stops without the mouse's movement seem more spread out and I'm not sure if that's the global variables or the randoms at work.
Edit: I've fixed the problem where the program only runs when the mouse moves by unindenting:
for foo in range(len(particles)):
particles[foo].update()
gm.draw.circle(main, (0, 0, 0), (int(particles[foo].pos_x), int(particles[foo].pos_y)), 5, 0)
However, the particles still seem to hardly move from their own positions, oscilating locally.
Problem 1 was solved thanks to Marius. I fixed it by simply putting update outside the event loop.
Problem 2 was fixed by adding the local update that I forgot.
if self.pos_z < global_best[2]: # This was not forgotten
global_best = [self.pos_x, self.pos_y, self.pos_z]
if self.pos_z < local_best[2]: # This was forgotten
local_best = [self.pos_x, self.pos_y, self.pos_z]

Python - spherical coordinates have Z axis bias

Long story short I need to get a bunch of balls moving in random directions in space. I am using a python script with VRED (3D rendering software) to display the balls on screen.
I am trying to use spherical coordinates but somehow the distribution of the balls in space is biased to the Z axis. I really cannot figure out where I got it wrong.
I am proceeding like this:
I generate a random yaw starting direction (-180, 180) and a random starting pitch (0, 180)
At each new frame I change the yaw and pitch by a small amount and move the ball in that new direction.
Here is my Python code (I hope it's not too hard to read; vrAEBase is a class related to VRED which allows the loop() to be updated each frame):
import random
import math
populationsize = 1000
balllist = []
#________________________________________BALL CLASS______________________________________________
class Ball(vrAEBase):
def __init__(self):
vrAEBase.__init__(self)
self.addLoop()
self.body = createSphere(2,100,1,1,1) # create sphere
self.isplaying = false
self.steplength = 20 #step length between each frame
self.yaw = random.uniform(-180, 180) #set starting yaw
self.pitch = random.uniform(0, 180) #set starting pitch
self.maxsteering = 1 # max angular change for yaw/pitch for each frame
self.x = 0 #startting X location
self.y = 0 #startting Y location
self.z = 0 #startting Z location
def loop(self): #loop is executed every frame
if self.isplaying:
self.yaw = self.yaw + random.uniform(-1*self.maxsteering, self.maxsteering) #set new yaw
self.pitch = self.pitch + random.uniform(-1*self.maxsteering, self.maxsteering) #set new pitch
localX = self.steplength * (math.sin(self.pitch)) * (math.cos(self.yaw)) #calculate X step length
localY = self.steplength * (math.sin(self.pitch)) * (math.sin(self.yaw)) #calculate Y step length
localZ = self.steplength * (math.cos(self.pitch)) #calculate Z step length
self.x += localX
self.y += localY
self.z += localZ
setTransformNodeTranslation(self.body, self.x,self.y,self.z,true)
def rewind(self):
self.isplaying = false
self.x = 0
self.y = 0
self.z = 0
setTransformNodeTranslation(self.body, self.x,self.y,self.z,true)
#__________________________________PLAY__________________________________
def play():
global balllist
for ball in balllist:
if ball.isplaying == false:
ball.isplaying = true
else:
ball.isplaying = false
#__________________________________REWIND_________________________________
def rewind():
global balllist
for ball in balllist:
ball.rewind()
#_______________________________SPAWN BALLS________________________________
for x in range(0, populationsize):
newball = Ball() #create ball
balllist.append(newball) #add ball to list
play()
print("end")
Here is an image of the final distribution:
The problem is that in order to generate a uniform distribution of points around a sphere you cannot do phi = [0,pi] and theta=[-pi,pi] as this would lead to a surface elment dA= dphi*dtheta instead of the correct one dA= sin(phi)*dphi*dtheta.
In order to achieve the correct volume elment change
def __init__( self):
...
self.yaw = random.uniform(-180, 180) #set starting yaw
self.pitch = random.uniform(0, 180) #set starting pitch
...
to
def __init__( self):
...
u = random.uniform(0,1)
v = random.uniform(0,1)
self.yaw = 2 * math.pi * u #set starting yaw
self.pitch = math.acos( 2*v -1) #set starting pitch
...
For more documentation see http://mathworld.wolfram.com/SpherePointPicking.html.
Also be careful of the behavior of your timestep routine, as of now it seems that the dots will tend to collapse more toward this distribution. I don't know if this is your intended behavior

While loop not correctly stopping as it should

I am writing a little program that is intended to draw a line 25 pixels wherever the randrange will give it it's points. I also have 4 red boxes that are acting as bombs or mines. When the x,y of the line is red by the getColor function, then the var 'color' will be == to red. Thus stopping the while loop, which will stop the line from continuing on. This is also the same desired function for the blue dots I have drawn on the playing field. I have found that my program is not functioning this way unfortunately.Any suggestions on how I could fix this?
from random import *
def main( ):
#draw
pic = makeEmptyPicture(600, 600, white)
show(pic)
#for the 4 boxes
boxCount = 0
#while statement to draw
while boxCount < 4:
addRectFilled(pic, randrange(0,576), randrange(0,576), 25, 25, red)
addArcFilled(pic, randrange(0,576), randrange(0,576), 10, 10, 0, 360, blue)
boxCount = boxCount + 1
repaint(pic)
#vars for while statement
newX = 0
newY = 0
oldX = 0
oldY = 0
robotcount = 0
finished = 0
safe = 0
triggered = 0
#while loop, stops # step 750, or when a px == red/blue
while robotcount < 750 or color == red or color == blue:
oldX = newX
oldY = newY
#how to generate a new line poing +25/-25
newX = newX + randrange(-25, 26)
newY = newY + randrange(-25, 26)
#if statements to ensure no x or y goes over 599 or under 0
if newX > 599 or newX < 0:
newX = 0
if newY > 599 or newY < 0:
newY = 0
#functions to get pixel color of x,y
px = getPixel(pic, newX, newY)
color = getColor(px)
#draw the line from old to new, and also add +1 count for robot's steps
addLine(pic, oldX, oldY, newX, newY, black)
robotcount = robotcount + 1
#if statement to determine why the while loop stops
if color == red:
triggered = 1
printNow("trig")
if color == blue:
safe = 1
printNow("safe")
if robotcount == 750:
finished = 1
printNow("Fin")
You would like to achieve this:
#while loop, stops # step 750, or when a px == red/blue
This does not work:
while robotcount < 750 or color == red or color == blue:
It would be simpler to use a forloop instead:
for robotcount in range(750):
if color == red or color == blue:
break
You can also use a while loop and fix your conditions (note the !=):
while robotcount < 750 or color != red or color != blue:

Categories

Resources