#๐Ÿ”’ logic/math error trig

24 messages ยท Page 1 of 1 (latest)

late torrentBOT
#

@azure scarab

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

azure scarab
#
#class
class Ball(pygame.sprite.Sprite):
    def __init__(self,im,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image=im
        self.rect=self.image.get_rect()
        self.rect.x=x
        self.rect.y=y
        #physics stuff
        self.velocity=1
        self.velocityBearing=110
        self.acceleration=9.8
        self.accelerationBearing=180
    def addNewVelocity(self,bearing,magnitude):
        x=math.cos(math.radians(self.velocityBearing))*self.velocity
        y=-math.sin(math.radians(self.velocityBearing))*self.velocity
        x2=math.cos(math.radians(bearing))*magnitude
        y2=-math.sin(math.radians(bearing))*magnitude
        xtot=x+x2
        ytot=y+y2
        nbearing=math.atan2(ytot,xtot)
        nmagnitude=math.sqrt(xtot**2+ytot**2)
        print(nbearing,nmagnitude)
        return nbearing,nmagnitude
        
        
        
    def update(self):
        
        dx=(math.sin(math.radians(self.velocityBearing))*self.velocity)
        dy=-(math.cos(math.radians(self.velocityBearing))*self.velocity)
        ddx=(math.sin(math.radians(self.accelerationBearing))*self.acceleration)
        ddy=(math.cos(math.radians(self.accelerationBearing))*self.acceleration)
        self.velocityBearing,self.velocity=self.addNewVelocity(self.accelerationBearing,self.acceleration)
        self.rect.x+=dx
        self.rect.y+=dy
        
        
        if self.rect.right > WIDTH:

            print("off screen!")

#

it renders fine so this is the bit u would need its a math/logic error where instead of having a smooth motion as it slowly gets closer and closer to the bearing of 180 falling down as i wanted it shoots up

#

its related to the addNewVelocity function i think as thats the new thing that doesnt seem to be working

fresh elbow
#

pro tip: doing all that trig for every screen update will be costly

azure scarab
#

how else would i do it then

fresh elbow
#

You'll have a better time storing and working with vectors in their cartesian components as opposed to polar format

#

so instead of velocityBearing and velocity magnitude, keep track of velocity as a vector of X and Y mag

azure scarab
#

ye thats prob a good idea lol

#

do you see whats wrong with it tho

fresh elbow
#

Nope, the trig is just what stood out to me

#

Honestly I would try switching to pure Cartesian and see where you get

#

You'll find it's much simpler.

azure scarab
#

okedoki

#

thats the x and y isnt it

#

i did it

#

this is sick

#

my computin teacher will love this im sure we started pygame like 2 lessons ago im cookin trust

azure scarab
#

i think everything was right in my other i just realised that acceleration unlike in physics to go up needs to be negative and in physics its negative to go downwards lol

#

also my values were too high i think cuz its happening 60 times a second in mine

fresh elbow
#

Nice. Good to hear

late torrentBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.