#๐ logic/math error trig
24 messages ยท Page 1 of 1 (latest)
@azure scarab
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.
#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
pro tip: doing all that trig for every screen update will be costly
how else would i do it then
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
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.
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
thank you so much genuinely
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
Nice. Good to hear
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.