[Pygame] Simulating a tilting/falling pole

[Pygame] Simulating a tilting/falling pole

Postby hansn » Sat Apr 27, 2013 6:44 pm

I'm trying to simulate a falling pole, or rather a pygame.draw.line, but I can't make it look right. The line keeps changing lenght and width during the tilt.

I've been working on this for a while, I'm trying to create a simple tank shooter and I'm stuck at making the canons moveable.

It seems like a stupid question, but do you know how I can do this?

Try this simple script if you don't understand what I am trying to do.

Code: Select all
import sys, pygame

pygame.init()
SIZE = width, height = 600, 400
screen = pygame.display.set_mode(SIZE)
pos = [300, 200]
horizontal_direction = 1
vertical_direction = -1
clock = pygame.time.Clock()
while 1:
    clock.tick(50)
    screen.fill((255, 100, 150))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    if pos[0] == 300:
        vertical_direction *= -1
   
    if pos[0] > 300:
        if horizontal_direction == 1 and pos[1] >= height:
            horizontal_direction *= -1
            vertical_direction *= -1
    if pos[0] < 300:
        if horizontal_direction == -1 and pos[1] >= height:
            horizontal_direction *= -1
            vertical_direction *= -1

    pygame.draw.line(screen, (0,0,0), (300, 400), pos, 5)

    pos[0] += horizontal_direction
    pos[1] += vertical_direction
   
   
    pygame.display.update()
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: [Pygame] Simulating a tilting/falling pole

Postby metulburr » Sat Apr 27, 2013 7:55 pm

I can't make it look right. The line keeps changing lenght and width during the tilt.

This isnt moving back and forth sweeping, but you get the idea


Code: Select all
import pygame
import math

pygame.init()

size = (400,400)
screen = pygame.display.set_mode(size)
my_clock = pygame.time.Clock()
angle = 0
run = True

while run:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         run = False
   screen.fill((255,255,255))
   box_dimensions = (20,20,250,250)
   pygame.draw.ellipse(screen, (255,0,0), box_dimensions, 2)
   x = 125 * math.sin(angle) + 145
   y = 125 * math.cos(angle) + 145
   pygame.draw.line(screen, (0,0,255), (145,145), (x,y), 2)
   angle = angle + .05

   if angle > (2 * math.pi):
      angle = angle - (2 * math.pi)
   pygame.display.flip()
   my_clock.tick(20)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1509
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] Simulating a tilting/falling pole

Postby hansn » Sat Apr 27, 2013 8:09 pm

Thanks for the quick reply.

I guess it's time to learn what the sin and cos functions do.


After making the canons in my tank game turn I'm going to implement the canon-balls. I'm currently reading about RK4 to make the physics right, got any suggestions on other stuff I should read on that subject?
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: [Pygame] Simulating a tilting/falling pole

Postby Mekire » Sun Apr 28, 2013 2:36 am

You can't make games if you don't at least know some rudimentary trigonometry. That is just the way it is.

Code: Select all
import sys, pygame
import math

pygame.init()
SIZE = 600,400
screen = pygame.display.set_mode(SIZE)

start = (300,400)
angle = 270
radius = 200
fall_right = True
clock = pygame.time.Clock()

while 1:
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
            pygame.quit();sys.exit()

    if fall_right:
        angle += 1
    else:
        angle -= 1

    if not (180 <= angle <= 360):
        fall_right = not fall_right
    ang = math.radians(angle)
    end = radius*math.cos(ang)+300,radius*math.sin(ang)+400

    screen.fill(-1)
    pygame.draw.line(screen, (0,0,0),start,end,5)
    pygame.draw.circle(screen,(255,0,0),(300,400),200,3)
    pygame.display.update()
    clock.tick(50)

-Mek
User avatar
Mekire
 
Posts: 1018
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] Simulating a tilting/falling pole

Postby Mekire » Sun Apr 28, 2013 2:42 am

hansn wrote:I'm currently reading about RK4 to make the physics right

Honestly I would say that this early on you should probably first try to implement it manually. Learn some trig; learn basic kinematic equations (physics); and see how far you can get. Like I said, physics engine or not, as a game programmer you need to know basic trigonometry (at the very least you should know the formula for a circle).

-Mek
User avatar
Mekire
 
Posts: 1018
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] Simulating a tilting/falling pole

Postby hansn » Sun Apr 28, 2013 7:47 am

Mekire wrote:
hansn wrote:I'm currently reading about RK4 to make the physics right

Honestly I would say that this early on you should probably first try to implement it manually. Learn some trig; learn basic kinematic equations (physics); and see how far you can get. Like I said, physics engine or not, as a game programmer you need to know basic trigonometry (at the very least you should know the formula for a circle).

-Mek

I'll do that. Thanks
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: [Pygame] Simulating a tilting/falling pole

Postby hansn » Sun Apr 28, 2013 7:46 pm

Putting what I read about trigonometry into context with my programming is hard, but I'm sure I will get it eventually. In the mean time, you know of any good reads on the subject?
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: [Pygame] Simulating a tilting/falling pole

Postby Mekire » Mon Apr 29, 2013 9:33 am

I don't know of any resources that specifically try to tie the two subjects together, but that is unimportant. The coordinates in pygame are a rectangular/cartesian coordinate system. For starters you should be comfortable with finding the angles of right triangles given two sides of the triangle, or finding a side of the triangle given the angle and one other side. You should know what SOH CAH TOA means. You should understand how to represent a circle in functions. You definitely need to understand the Pythagorean theorem but this goes without saying. Also you should be able to solve a basic quadratic equation if necessary.

Once you get to a more advanced level linear algebra becomes a very useful skill although admittedly I need to refresh on it myself. Some knowledge of calculus would be nice especially when doing physics related calculations, but you will be able to make do without it for a long time.

-Mek
User avatar
Mekire
 
Posts: 1018
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] Simulating a tilting/falling pole

Postby metulburr » Mon Apr 29, 2013 1:47 pm

You should know what SOH CAH TOA means

Some old hippy caught another hippy tripping on acid.

lol
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1509
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] Simulating a tilting/falling pole

Postby Mekire » Mon Apr 29, 2013 2:13 pm

What ever works :mrgreen: .

Don't know if you are familiar with calculus but a teacher of mine had one for remembering the quotient rule:
Image

"Low dee-high,
less high dee-low,
draw the line,
and square below."


Needless to say I haven't forgotten it since.

-Mek
User avatar
Mekire
 
Posts: 1018
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests