Author Topic: My physics game  (Read 20931 times)

0 Members and 1 Guest are viewing this topic.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #30 on: February 01, 2011, 08:27:40 pm »
I need to get this to work efficiently this is looking harder and harder every second. I could use any help with optimization. I almost have some gravity working. It is just a little to fast right now. Any way for me to slow it down a little. It is at 1 pixel per frame per frame at 25 frames per second.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #31 on: February 01, 2011, 08:48:02 pm »
I would recommend an acceleration under 1 pxl/f/f.  Also, If this is one of your first delve into physics, I would recommend not mixing physics until you are extremely well versed in them.  Mixing sprites and cellular automata is difficult even for some of the most advanced users.  Maybe stick with one or the other while you get more knowledgeable in physics?  While my tutorials might be good, no amount of tutorials in the world are supplement for practice.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #32 on: February 01, 2011, 09:12:35 pm »
i know most of the physics of this I just don't know the coding of it

here is my code I have commented out some of the sections I am currently working on. Again I still would appreciate all input on optimization and improvements for my code. My main problem right now is gravity is too fast. after I finish this next I will start on adding collision detection.
Code: [Select]
#import modules
import pygame, math, random, os
from pygame.locals import *

#Check to see if pygame is working right
if not pygame.font:
    print 'Warning, fonts disabled'
if not pygame.mixer:
    print 'Warning, sound disabled'

#define any needed global variables here

   
#Create a function to load images for sprites.
def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    #If an error exit
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

#create a function to load sound
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join('data', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    #if an error exit
    except pygame.error, message:
        print 'Cannot load sound:', fullname
        raise SystemExit, message
    return sound

#classes for our game objects
class Player(pygame.sprite.Sprite):
    #moves a clenched fist on the screen, following the mouse
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('sprite.bmp', -1)
        self.direction=0
        self.downforce=0

    def update(self):
        #move the player based on direction chosen
        self.gravity()
        if self.direction == 0:
            newpos = self.rect.move((0, self.downforce))
            self.rect = newpos
        elif self.direction == 1:
            self.downforce-=2
            newpos = self.rect.move((0, self.downforce))
            self.rect = newpos
            self.downforce+=2
        elif self.direction == 2:
            self.downforce+=2
            newpos = self.rect.move((0,self.downforce))
            self.rect = newpos
            self.downforce-=2
        elif self.direction == 3:
            newpos = self.rect.move((-2, self.downforce))
            self.rect = newpos
        elif self.direction == 4:
            newpos = self.rect.move((2, self.downforce))
            self.rect = newpos
##        elif self.direction == 5:
##            newpos = self.rect.move((-2, -2))
##            self.rect = newpos
##        elif self.direction == 6:
##            newpos = self.rect.move((2, -2))
##            self.rect = newpos
##        elif self.direction == 7:
##            newpos = self.rect.move((-2, 2))
##            self.rect = newpos
##        elif self.direction == 8:
##            newpos = self.rect.move((2, 2))
##            self.rect = newpos

    def gravity(self):
        #need a collision detector here
        self.downforce+=1

    def up(self):
        self.direction=1

    def down(self):
        self.direction=2

    def left(self):
        self.direction=3

    def right(self):
        self.direction=4

    def still(self):
        self.direction=0

##   def upleft(self):
##        self.direction=5
##
##    def upright(self):
##        self.direction=6
##
##    def downleft(self):
##        self.direction=7
##
##    def downright(self):
##        self.direction=8

#function to load the game engine
def game_engine():
    #nothing done here yet
    return

#function for playing the game
def play_game():
    #load the game engine
    game_engine()
    #start the game
    #Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((975, 325))
    pygame.display.set_caption('Physics!')
    pygame.mouse.set_visible(1)
    turn = 0
    win = False
    lose = False
    achievements = 0

    #Create The Backgound
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

    #Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

    #Prepare Game Objects
    clock = pygame.time.Clock()
    player = Player()
    allsprites = pygame.sprite.RenderPlain((player))
    #Main Loop
    while 1:
        clock.tick(25)

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return
##            elif event.type == KEYDOWN and event.key == K_UP and event.key == K_LEFT:
##                player.upleft()
##            elif event.type == KEYDOWN and event.key == K_UP and event.key == K_RIGHT:
##                player.upright()
##            elif event.type == KEYDOWN and event.key == K_DOWN and event.key == K_LEFT:
##                player.downleft()
##            elif event.type == KEYDOWN and event.key == K_DOWN and event.key == K_RIGHT:
##                player.downright()
            elif event.type == KEYDOWN and event.key == K_UP:
                player.up()
            elif event.type == KEYDOWN and event.key == K_DOWN:
                player.down()
            elif event.type == KEYDOWN and event.key == K_LEFT:
                player.left()
            elif event.type == KEYDOWN and event.key == K_RIGHT:
                player.right()
            else:
                player.still()
               
               

        allsprites.update()

    #Draw Everything
        screen.blit(background, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()

def main():
    #define needed local variables here
   
    #start game
    play_game()
   
if __name__ == '__main__':
    main()
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: My physics game
« Reply #33 on: February 01, 2011, 09:33:20 pm »
Change the gravity to under 1p/f/f. Even on the calculator that's too fast, and computers run even faster. Use 1 over some very large number to slow it down.
In-progress: Graviter (...)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #34 on: February 01, 2011, 09:34:05 pm »
I didn't think you could work with partial pixels

EDIT: I tried doing 1/25 instead of 1 and it didn't work
EDIT 2: I fixed it I had put it as 1/25 when it needed to be 1/25.0 It is working now at a much more manageable speed
EDIT3:I also made it so that the sprite stops falling when it hits the bottom. Now I need to start on scenery and collision detection.
« Last Edit: February 01, 2011, 11:22:53 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: My physics game
« Reply #35 on: February 02, 2011, 12:57:20 am »
Awesome :) Keep us posted ^^

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #36 on: February 02, 2011, 02:53:48 pm »
I now have platforms randomly falling from the sky with a slightly slower gravity(assuming a large air resistance)1/12.5fp/s/s. I have them generate randomly in the sky at a random spot and fall. I have also made the jumping for the player slightly more realistic. You can't jump while you are in the air. The gravity for the player is 1/15fp/s/s. I will post my code in just a little bit.

EDIT: I have a question do you guys think I should make it impossible to change the direction of a jump once your in the air? I have tried to implement this and failed so I'll keep trying if you guys find it a good idea I'll quit if not. Here is my code:
Code: [Select]
#import modules
import pygame, math, random, os
from pygame.locals import *

#Check to see if pygame is working right
if not pygame.font:
    print 'Warning, fonts disabled'
if not pygame.mixer:
    print 'Warning, sound disabled'

#define any needed global variables here

   
#Create a function to load images for sprites.
def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    #If an error exit
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

#create a function to load sound
def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join('data', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    #if an error exit
    except pygame.error, message:
        print 'Cannot load sound:', fullname
        raise SystemExit, message
    return sound

class Platform(pygame.sprite.Sprite):
    #moves a clenched fist on the screen, following the mouse
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('platform.bmp')
#        self.area=screen.get_rect()
        self.downforce=0
        self.dead=1
        self.rect.topleft

    def update(self):
        #move the player based on direction chosen
        if self.dead==0:
            self.gravity()
            newpos=self.rect.move((0,self.downforce))
            self.rect=newpos

    def gravity(self):
        if self.rect.bottom>615:
            self.downforce=0
            self.dead=1
        else:
            self.downforce+=1/12.5
   
    def alive(self):
        self.dead=0
        self.initial=random.randint(0,750)
        self.rect.topleft=self.initial, 1
       


#classes for our game objects
class Player(pygame.sprite.Sprite):
    #moves a clenched fist on the screen, following the mouse
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image, self.rect = load_image('sprite.bmp')
        self.rect.topleft=1, 558
        self.direction=0
        self.downforce=0
        self.jump=0
        self.change=0
#        self.area=screen.get_rect()

    def update(self):
        #move the player based on direction chosen
        self.gravity()
        if self.direction == 0:
            newpos = self.rect.move((0, self.downforce))
        elif self.direction == 1:
            if self.jump==0 or self.rect.bottom>=599:
                self.jump=1
                self.downforce-=3.5
            newpos = self.rect.move((0, self.downforce))
        elif self.direction == 3:
            newpos = self.rect.move((-4, self.downforce))
        elif self.direction == 4:
            newpos = self.rect.move((4, self.downforce))
        elif self.direction == 5:
            newpos = self.rect.move((-2, -2))
        elif self.direction == 6:
            newpos = self.rect.move((2, -2))
        self.rect = newpos

    def gravity(self):
        #need a collision detector here
        if self.rect.bottom>=599:
            self.downforce=0
            self.jump=0
            self.change=0
            self.rect.bottom=599
        else:
            self.downforce+=1/15.0

    def up(self):
        if self.jump==0:
            self.direction=1

    def left(self):
        self.direction=3

    def right(self):
        self.direction=4

    def still(self):
        self.direction=0

    def upleft(self):
        self.direction=5

    def upright(self):
        self.direction=6


#function to load the game engine
def game_engine():
    #nothing done here yet
    return

#function for playing the game
def play_game():
    #load the game engine
    game_engine()
    #start the game
    #Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((900, 600))
    pygame.display.set_caption('Physics!')
    pygame.mouse.set_visible(1)
    turn = 0
    win = False
    lose = False
    achievements = 0

    #Create The Backgound
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

    #Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

    #Prepare Game Objects
    clock = pygame.time.Clock()
    player = Player()
    platforma = Platform()
    platformb = Platform()
    platformc = Platform()
    platformd = Platform()
    platforme = Platform()
    allsprites = pygame.sprite.RenderPlain((player, platforma, platformb, platformc, platformd, platforme))
    #Main Loop
    while 1:
        clock.tick(25)

        #Generate Events
        rand=random.randint(1,250)
        if rand==20 and platforma.dead==1:
            platforma.alive()
        if rand==10 and platformb.dead==1:
            platformb.alive()
        if rand==30 and platformc.dead==1:
            platformc.alive()
        if rand==40 and platformd.dead==1:
            platformd.alive()
        if rand==75 and platforme.dead==1:
            platforme.alive()

    #Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                pygame.quit()
                return
            if event.type == KEYDOWN and event.key == K_UP and event.key == K_LEFT:
                player.upleft()
            elif event.type == KEYDOWN and event.key == K_UP and event.key == K_RIGHT:
                player.upright()
            elif event.type == KEYDOWN and event.key == K_UP:
                player.up()
            elif event.type == KEYDOWN and event.key == K_LEFT:
                player.left()
            elif event.type == KEYDOWN and event.key == K_RIGHT:
                player.right()
            else:
                player.still()

        allsprites.update()

    #Draw Everything
        screen.blit(background, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()

def main():
    #define needed local variables here
   
    #start game
    play_game()
   
if __name__ == '__main__':
    main()
Again I still need ideas for improvements and optimizations. thank you for any help you might give.

EDIT 2: I redid the description above to add more detail and bring it to where I currently am
« Last Edit: February 02, 2011, 03:38:14 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: My physics game
« Reply #37 on: February 02, 2011, 04:17:41 pm »
For cellular automata... some searching can be done for pixels. Or maybe.... you could create arrays with locations WITH the water! ;)
(It is weird, and that's the only idea I could come up with.)

Code sample:
Code: [Select]
# Python "water" pixel (cellular automata) code sample
pixwaterarray = []
pixwaterarray.append([123,456], [111, 222], [333, 444]) # This should be populated with values that are isolated to a certain area to begin.

for arr in pixwaterarray:
   yloc = arr[1]
   # Lookup if there are pixels below it...
   nobelow = 1
   for arr2 in pixwaterarray:
      if arr2[1] > yloc:
         nobelow = 0
   if nobelow = 1:
      pixwaterarray.remove(arr)
      pixwaterarray.append([arr[0], yloc + 1])

# Then you can loop through the array again and then replot the pixels! :)

EDIT: I realized you code and post faster than me! :P You should make sure to do checks with files, such as the sprites. Also, if possible, include the sprite in question as an attachment! :)
« Last Edit: February 02, 2011, 04:20:04 pm by alberthrocks »
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #38 on: February 02, 2011, 04:22:03 pm »
Right now what I need is a good collision detection function. I can't do much without that.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline alberthrocks

  • Moderator
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 876
  • Rating: +103/-10
    • View Profile
Re: My physics game
« Reply #39 on: February 02, 2011, 04:28:40 pm »
Right now what I need is a good collision detection function. I can't do much without that.
Not good at that, sorry. :( I haven't really done much other than play around with effects and pixel modding.

http://www.switchonthecode.com/tutorials/collision-detection-with-pygame seems to be a decent tutorial on collision detection.

Also, from the official Pygame "newbie" guide:
Quote from: Pygame "Newbie" Guide, slightly modified
9. Rects are your friends.

Pete Shinners' wrapper may have cool alpha effects and fast blitting speeds, but I have to admit my favorite part of pygame is the lowly Rect class. A rect is simply a rectangle - defined only by the position of its top left corner, its width, and its height. Many pygame functions take rects as arguments, and they also take 'rectstyles', a sequence that has the same values as a rect. So if I need a rectangle that defines the area between 10, 20 and 40, 50, I can do any of the following:

rect = pygame.Rect(10, 20, 30, 30)
rect = pygame.Rect((10, 20, 30, 30))
rect = pygame.Rect((10, 20), (30, 30))
rect = (10, 20, 30, 30)
rect = ((10, 20, 30, 30))
If you use any of the first three versions, however, you get access to Rect's utility functions. These include functions to move, shrink and inflate rects, find the union of two rects, and a variety of collision-detection functions.

For example, suppose I'd like to get a list of all the sprites that contain a point (x, y) - maybe the player clicked there, or maybe that's the current location of a bullet. It's simple if each sprite has a .rect member - I just do:

sprites_clicked = [sprite for sprite in all_my_sprites_list if sprite.rect.collidepoint(x, y)]
Rects have no other relation to surfaces or graphics functions, other than the fact that you can use them as arguments. You can also use them in places that have nothing to do with graphics, but still need to be defined as rectangles. Every project I discover a few new places to use rects where I never thought I'd need them.

10. Other collision detection methods

So you've got your sprites moving around, and you need to know whether or not they're bumping into one another. It's tempting to write something like the following:

Check to see if the rects are in collision. If they aren't, ignore them.
For each pixel in the overlapping area, see if the corresponding pixels from both sprites are opaque. If so, there's a collision.
There are other ways to do this, with ANDing sprite masks and so on, but any way you do it in pygame, it's probably going to be too slow. For most games, it's probably better just to do 'sub-rect collision' - create a rect for each sprite that's a little smaller than the actual image, and use that for collisions instead. It will be much faster, and in most cases the player won't notice the inprecision.

Hope this helps! :D
« Last Edit: February 02, 2011, 04:29:47 pm by alberthrocks »
Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/


Proud member of ClrHome!

Miss my old signature? Here it is!
Spoiler For Signature:
Alternate "New" IRC post notification bot (Newy) down? Go here to reset it! http://withg.org/albert/cpuhero/

Withgusto Networks Founder and Administrator
Main Server Status: http://withg.org/status/
Backup Server Status: Not available
Backup 2/MC Server Status: http://mc.withg.org/status/

Activity remains limited due to busyness from school et al. Sorry! :( Feel free to PM, email, or if you know me well enough, FB me if you have a question/concern. :)

Don't expect me to be online 24/7 until summer. Contact me via FB if you feel it's urgent.


Proud member of ClrHome!

Spoiler For "My Projects! :D":
Projects:

Computer/Web/IRC Projects:
C______c: 0% done (Doing planning and trying to not forget it :P)
A_____m: 40% done (Need to develop a sophisticated process queue, and a pretty web GUI)
AtomBot v3.0: 0% done (Planning stage, may do a litmus test of developer wants in the future)
IdeaFrenzy: 0% done (Planning and trying to not forget it :P)
wxWabbitemu: 40% done (NEED MOAR FEATURES :P)

Calculator Projects:
M__ C_____ (an A____ _____ clone): 0% done (Need to figure out physics and Axe)
C2I: 0% done (planning, checking the demand for it, and dreaming :P)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #40 on: February 03, 2011, 06:48:38 pm »
I think I have collision detection working now. Does anyone know how to realistically simulate projectiles launched at an angle? I need that for the next part of my game. I will post my collision code later. I have redone some other parts for efficiency/ease to read/access
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: My physics game
« Reply #41 on: February 03, 2011, 07:11:32 pm »
Keep track of the projectiles x and y position as well as the x and y velocity. increment position by velocity every frame, and increase y by gravity every frame (I think you know this already :P) When it's fired, the initial x and y velocities should be (finalvelocity)*cos(angle) and (finalvelocity)*sin(angle) respectively

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #42 on: February 03, 2011, 07:14:23 pm »
I need a way To realistically simulate projectile movement will that work for a frame by frame movement and if so how? Sorry if I seem stupid this is my first work with projectiles.(I really have done no real big games I just developed a few small ones)
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: My physics game
« Reply #43 on: February 03, 2011, 07:21:44 pm »
I don't know python so I can only kinda explain the process...anyway, what you need first is the angle, and the speed that you want it fired. So let's say we want an angle of 30 degrees at a speed of, say, 5. (I will use BASIC/Axe style syntax here)
So, to initialize the speed of the projectile (in variables I'll call xvel and yvel) we will do 5cos30->xvel and 5sin30->yvel.
Now, every frame we will change the position of the projectile by the speed of it (makes sense, right? if we are traveling at 5 units per frame the next frame we will have moved by 5). So xpos+xvel->xpos, and ypos+yvel->ypos
But the projectile is affected by gravity, right? So whatever our gravity is (let's pretend it's an acceleration of 1 unit per frame) we will need to change the y speed by that value. So yvel-1->yvel

Now do this every frame and you can use xpos and ypos as the location of where to draw the projectile :) Hope this helps

Think of the projectile as something that is affected by gravity, but also moves horizontally.
« Last Edit: February 03, 2011, 07:25:19 pm by squidgetx »

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My physics game
« Reply #44 on: February 03, 2011, 07:29:35 pm »
How would you calculate the changing angle as it moved through the air. This is my main problem i get the rest of it though
(even though it was in a different language :'( )
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y