Trigonometry

Started by
13 comments, last by Fla5hbang 11 years, 1 month ago
I found I pygame library called pymunk that's supposed to help out with physics and trig I downloaded it a checked out the examples that came with it, it looks really helpfull but doesnt explain much so I guess I'm gonna look more into pygames vectors
Advertisement

Looks like pymunk tutorial is here:

https://code.google.com/p/pymunk/wiki/SlideAndPinJointsExample

However looks like pymunk uses compiled C lib, my knowledge ends here.

Looks like I'm working with vectors then
ok i went back to the pymunk site printed out the examples,read through the library and yanked out the highlighters! i figured out how to rotate and walk the little circle around! heres my code (sorry if im not supposed to post code here)


#player move

# I - IMPORT---------------------------------------

import sys

import pygame
from pygame.locals import *
from pygame.color import *

import pymunk
from pymunk.vec2d import Vec2d
from pymunk.pygame_util import draw_space, from_pygame



# D - DISPLAY CONFIGURATION-----------------------------------
width, height = 690,600
screen = pygame.display.set_mode((width,height))
WHITE = (255,255,255)


# E - ENTITIES ----------------------------------
space = pymunk.Space() #space is kinda like pygames surface

# "player"
PLAYER_body = pymunk.Body(pymunk.inf, pymunk.inf)
PLAYER_shape = pymunk.Circle(PLAYER_body, 25)
PLAYER_shape.sensor = True
PLAYER_body.position = 100,100
space.add(PLAYER_shape)

# A - ACTION (BROKEN INTO ALTER STEPS)---------------------------

# A - ASSIGN VALUES TO KEY VARIABLES------------------------

PLAYER_VELOCITY = 100. *2.

PLAYER_body.position = 100,100

clock = pygame.time.Clock()

def main():
pygame.init()

running = True

# L - SET UP MAIN GAME LOOP--------------------------------
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False

keys = pygame.key.get_pressed()
target_vx = 0

speed = 2.5
if (keys[K_UP]):
PLAYER_body.position += Vec2d(0,1) * speed
if (keys[K_DOWN]):
PLAYER_body.position += Vec2d(0,-1) * speed
if (keys[K_LEFT]):
PLAYER_body.position += Vec2d(-1,0) * speed
if (keys[K_RIGHT]):
PLAYER_body.position += Vec2d(1,0) * speed


if (keys[K_LEFT]):
direction = -1
target_vx -= PLAYER_VELOCITY
if (keys[K_RIGHT]):
direction = 1
target_vx += PLAYER_VELOCITY
if (keys[K_DOWN]):
direction = -3

mouse_position = from_pygame( Vec2d(pygame.mouse.get_pos()), screen )
PLAYER_body.angle = (mouse_position - PLAYER_body.position).angle

# T - TIMER TO SET FRAME RATE-------------------------------
screen.fill(WHITE)
fps = 40
clock.tick(fps)


# E - EVENT HANDLEING--------------------------------------

draw_space(screen, space)


# R - REFRESH DISPLAY---------------------------------------
pygame.display.flip()


if __name__ == '__main__':
sys.exit(main())

Looks like pymunk tutorial is here:

https://code.google.com/p/pymunk/wiki/SlideAndPinJointsExample

thanks!

This topic is closed to new replies.

Advertisement