jdf / Processing.py-Bugs

A home for all bugs and feature requests about Python Mode for the Processing Development Environment.
41 stars 8 forks source link

PVectors losing magnitude #174

Closed pyNuts closed 7 years ago

pyNuts commented 7 years ago

For some reason using a PVector to make a ball bounce up and down always loses height over time and messes up after a few bounces, im not sure if its a fault with the python mode because its a pretty simple code.

Heres a simple example of what i mean, im new to classes but im pretty sure its not the code.

class Bounce: global location, velocity, acceleration

location = PVector(300,300)
velocity = PVector(0,0)
acceleration = PVector(0,1)

def update(self):
    velocity.add(acceleration)
    location.add(velocity)

def display(self):
    fill(0)
    ellipse(location.x, location.y, 20, 20)

def edge(self):
    if location.x <= 0 or location.x >= width-20:
        velocity.x *= -1
    if location.y <= 0 or location.y >= height-20:
        velocity.y *= -1

def setup(): global b size(600,600) b = Bounce()

def draw(): background(255) b.update() b.edge() b.display()

pyNuts commented 7 years ago

I think I've found a solution.

If I add both forces in the update function to a For Loop, with a range of the number of forces, it will run through both before applying them, so far it seems to be working and not loosing any velocity.

Still, can anyone explain why it needed that in the first place?

pyNuts commented 7 years ago

Well it worked for one example but now doesn't must be a bug.

jdf commented 7 years ago

There is an error in your algorithm. Is this homework, or just for fun?

pyNuts commented 7 years ago

Hi, its just fun, im kind of new to classes but have tried many ways and the ball keeps bleeding energy with each bounce, Ive tried the whole thing when location.y goes below height to set it as height before reversing velocity hoping that would help but no luck, any recommendations?

I did manage to fix the ball going off screen issue though with location.y = height but its the magnitude of the bounce that is losing energy, Ive read multiple sources but nothing has worked.

jdf commented 7 years ago

I believe the issues you're having are not a bug in Python Mode for Processing, so I'm closing the bug. Give the Processing forums a try for help with the bounce algorithm.