Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Odd physics bugs [FIXED]

edited January 2015 in Bugs Posts: 63

This game should in theory work correctly, and sometimes it does, but when playing you'll see that a couple of weird things happen. First of all, your character will occasionally be boosted by some invisible object in mid air, this object has been identified as the dynamic physics body: newtonyspeed (built as a dynamic body for the sake of upward and downward screen scrolling), which could not possibly be bumping with you because it has the fixed x coordinate of -1. The second occasional bug is one where you go up against the currently invisible right or left boundary and the object will go all fuzzy, even though upon hitting that boundary and pushing up against it, the blocks x linear velocity should be switched to zero. Another more serious bug is one where you will push up against one of the boundaries and upon tilting your screen away from it, you will be teleported across the screen, and then back to the boundary if you tilted it in the opposite direction after that. Another bug occurs when you give the floor body an info (floor.info="something"), your block will spawn immidiately against it and it will not start bouncing. There is also the sprite that should display an asset from cargo bot to fill the left side boundary (the one for the right boundary hasn't been inserted yet); you'll see that that sprite doesn't appear (which might actually be an error in my positioning). I ran through main syntax again and again. I mean the game tab is only 182 lines and I havnt gone into making anything really complicated. You also will not see the math.random function ANYWHERE in it so I do not understand how the bugs occurrences might be random. It would be great if someone could help me identify what causes them and how they can be avoided, or if they are just bugs with codea itself. Either way, any help is appreciated.

Game info: there is no current objective, you just bounce up when you hit the floor, and fall back down. Tilt to move the object, and touch/hold your finger on the screen to glide. There is a scrolling mechanism in this code (feel free to use it for reference in other platform projects), so the block never goes off screen. The equation for the backgrounds width is (devices height)*2. On that note, you will notice a lot of HEIGHT/(some weird number), that is for the sake of making the games code universal on all devices, with the size of each object varying based on the devices height in pixels.

Comments

  • Code:

    --Main class:
    supportedOrientations(PORTRAIT_ANY)
    function setup()
    parameter.watch("newtonyspeed.x")
    parameter.watch("stuff1center.y")
    parameter.watch("ymobility")
    parameter.watch("floor.y")
    parameter.watch("newtonxspeed")
    parameter.watch("newtonyspeed.linearVelocity.y")
    parameter.watch("newton.linearVelocity.x")
    parameter.watch("newton.linearVelocity.y")
    parameter.watch("stuff1center.y")
    parameter.integer("WIDTH",0,1536,768)
    parameter.integer("HEIGHT",0,2048,1024)
    displayMode(OVERLAY)
    x=TitleScreen()
    end
        function draw()
        x:draw()
    end
    function touched(t)
        x:touched(t)
    end
    
    --TitleScreen class: (only press start game when running this class)
    TitleScreen = class()
    function TitleScreen:init()
        rectMode(CORNER)
        textMode(CENTER)
        st=false
        cr=false
        he=false
    end
    function TitleScreen:draw()
        background(0, 72, 255, 255)
        fontSize(HEIGHT/51.2)
        local rectheight=HEIGHT/20.48
        local rectwidth=rectheight*4
        sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        stroke(0, 231, 255, 255)
        strokeWidth(HEIGHT/204.8)
        fill(8, 255, 0, 255)
        if st then
            fill(11, 0, 255, 255)
        end
        rect(WIDTH/2-HEIGHT/10.24,HEIGHT/2+HEIGHT/8.192,rectwidth,rectheight)
    
        fill(8, 255, 0, 255)
        if cr then
            fill(11, 0, 255, 255)
        end    
        rect(WIDTH/2-HEIGHT/10.24,HEIGHT/2-HEIGHT/40.96,rectwidth,rectheight)
    
        fill(8, 255, 0, 255)
        if he then
            fill(11, 0, 255, 255)
        end    
        rect(WIDTH/2-HEIGHT/10.24,HEIGHT/2-HEIGHT/5.85142857143,rectwidth,rectheight)
        fill(255, 0, 22, 255)
        text("Start Game",WIDTH/2,HEIGHT/2+HEIGHT/6.8)
        text("Credits",WIDTH/2,HEIGHT/2)
        text("Help",WIDTH/2,HEIGHT/2-HEIGHT/6.8)
        textSize(150,30)
    end
    
    function TitleScreen:touched(touch)
        if touch.x >= WIDTH/2-HEIGHT/10.24 and touch.x <= WIDTH/2+HEIGHT/10.24 and
            touch.y >= HEIGHT/2+HEIGHT/8.192 and touch.y <= HEIGHT/2+HEIGHT/5.85142857143 then
            if touch.state==BEGAN then
                st=true
            elseif touch.state==ENDED then
                x=Game()
            end
        end
        if touch.x >= WIDTH/2-HEIGHT/10.24 and touch.x <= WIDTH/2+HEIGHT/10.24 and 
            touch.y >= HEIGHT/2-HEIGHT/40.96 and touch.y <= HEIGHT/2+HEIGHT/40.96 then
            if touch.state==BEGAN then
                cr=true
            elseif touch.state==ENDED then
                x=Credits()
            end
        end
        if touch.x >= WIDTH/2-HEIGHT/10.24 and touch.x <= WIDTH/2+HEIGHT/10.24 and 
            touch.y >= HEIGHT/2-HEIGHT/5.85142857143 and touch.y <= HEIGHT/2-HEIGHT/8.192 then
            if touch.state==BEGAN then
                he=true
            elseif touch.state==ENDED then
                x=Help()
            end
        end
        if touch.state==ENDED then
            st=false
            cr=false
            he=false
        end
    end
    
    --Game class:
    --to do: use xmobility to determine scrolling, and xmobility only.
    Game=class()
    if newton~=nil then
        newton.destroy()
    end
    if stuff1center~=nil then
        stuff1center.destroy()
    end
    if floor~=nil then
        floor.destroy()
    end
    if newtonyspeed~=nil then
        newtonyspeed.destroy()
    end
    function Game:init()
        rectMode(CENTER)
        spriteMode(CENTER)
        textMode(CENTER)
        newtonsize=HEIGHT/28.1
        newton=physics.body(POLYGON,vec2(-newtonsize,newtonsize),vec2(-newtonsize,-newtonsize),vec2(newtonsize,-newtonsize),vec2(newtonsize,newtonsize))
        newton.type=DYNAMIC
        newton.info="main"
        newton.x=WIDTH/2
        newton.y=WIDTH/2
        stuff1center=physics.body(CIRCLE,1)
        stuff1center.x=WIDTH/2
        stuff1center.y=-1000
        stuff1center.type=KINEMATIC
        stuff1={}
        stuff2={}
        stuff3={}
        newtonxspeed=0
        newtonyspeed=physics.body(CIRCLE,1,1)
        newtonyspeed.type=DYNAMIC
        newtonyspeed.x=-1
        newtonyspeed.y=0
        newtonyspeed.info="how is this possibly coliding with you?"
        minheight1=0
        minheight2=-stuff1center.x
    floor=physics.body(EDGE,vec2(-1,stuff1center.y+1000),vec2(WIDTH,stuff1center.y+1000))
        floor.type=STATIC
        physics.gravity(0,-HEIGHT/4.096)
        xmobility=false
        left=false
        right=false
        ymobility=false
        up=false
        down=false
        touching=false
        yscroll=false
        xscroll=false
        rightwall=false
        leftwall=false
        xscrollboolean=true
    end
    
    function Game:draw()
        if newtonyspeed.linearVelocity.x~=0 or newtonyspeed.x~=-1 then
        newtonyspeed.x=-1
        end
        if glide==true then
            newtonyspeed.linearVelocity=vec2(0,HEIGHT/20.48)
        end
        background(0, 72, 255, 255)
        sprite("Cargo Bot:Game Area",stuff1center.x,stuff1center.y+1000+HEIGHT/2,HEIGHT*2,HEIGHT)
        sprite("Cargo Bot:Claw Arm",(stuff1center.x-HEIGHT+HEIGHT/10.24-newtonsize)/2,stuff1center.y+HEIGHT/2,HEIGHT/10.24-newtonsize,HEIGHT)
        sprite()
        pushStyle()
        fill(0, 255, 76, 255)
        ellipse(stuff1center.x,stuff1center.y+1000,20)
        line(-1,stuff1center.y+1000,WIDTH,stuff1center.y+1000)
        ellipse(newtonyspeed.x,newtonyspeed.y,20)
        popStyle()
        floor.y=stuff1center.y+1000
        fill(255, 19, 0, 255)
        rect(newton.x,newton.y,newtonsize*2,newtonsize*2)
        minheight1=stuff1center.x+minheight2
        --if newton goes below minheight1, the game will end. the minheight2 will become -stuff1center every time newton hits a platform so that minheight1 will be brought up to that point.
        newtonxspeed=Gravity.x*HEIGHT/4.096
        if stuff1center.x<=WIDTH/2-HEIGHT+WIDTH/2+HEIGHT/5.12 and newtonxspeed>=0 then
            stuff1center.x=WIDTH/2-HEIGHT+WIDTH/2+HEIGHT/5.12
            xmobility=true
            if newton.x>=WIDTH-HEIGHT/10.24 and newtonxspeed>0 then
                rightwall=true
            else rightwall=false
            end
        elseif stuff1center.x>=WIDTH/2+HEIGHT-WIDTH/2-HEIGHT/5.12 and newtonxspeed<=0 then
            stuff1center.x=WIDTH/2+HEIGHT-WIDTH/2-HEIGHT/5.12
            xmobility=true
            if newton.x<=HEIGHT/10.24 and newtonxspeed<0 then
                leftwall=true
            else leftwall=false
            end
        else
            if newton.x>=WIDTH/2+HEIGHT/5.12 and stuff1center.x>=WIDTH/2-HEIGHT and newtonxspeed>=0 then
                    xmobility=false
                    right=false
                    left=true
                    xscroll=true
            elseif newton.x<=WIDTH/2-HEIGHT/5.12 and stuff1center.x<=WIDTH/2+HEIGHT and newtonxspeed<=0 then
                xscroll=true
                xmobility=false
                left=false
                right=true
                stuff1center.linearVelocity.x=newtonxspeed
            elseif newton.x>=-HEIGHT and newton.x<=HEIGHT*2 then
                xmobility=true
                left=true
                right=true
                xscroll=false
            end
        end
        if newton.y>=HEIGHT/5.12 and newtonyspeed.linearVelocity.y>=0 then
            yscroll=true
            ymobility=false
            up=false
            down=true
            elseif newton.y<=newtonsize*2 and newtonyspeed.linearVelocity.y<=0 then
            yscroll=true
            ymobility=false
            down=false
            up=true
            else ymobility=true
            up=true
            down=true
            yscroll=false
        end
        if xmobility==true and ymobility==true then
            if leftwall==true and newton.linearVelocity.x<0  then
                newton.linearVelocity=vec2(0,newtonyspeed.linearVelocity.y)
                newton.x=HEIGHT/10.24
            elseif rightwall==true and newton.linearVelocity.x>0 then
                newton.linearVelocity=vec2(0,newtonyspeed.linearVelocity.y)
                newton.x=WIDTH-HEIGHT/10.24
            else
                newton.linearVelocity=vec2(newtonxspeed,newtonyspeed.linearVelocity.y)
            end
            stuff1center.linearVelocity=vec2(0,0)
        elseif xmobility==true and ymobility==false then
            if leftwall==true and newton.linearVelocity.x<0 then
                newton.linearVelocity=vec2(0,0)
                newton.x=HEIGHT/10.24
            elseif rightwall==true and newton.linearVelocity.x>0 then
                newton.linearVelocity=vec2(0,0)
                newton.x=WIDTH-HEIGHT/10.24
            else
                newton.linearVelocity=vec2(newtonxspeed,0)
            end
            stuff1center.linearVelocity=vec2(0,-newtonyspeed.linearVelocity.y)
        elseif xmobility==false and ymobility==true then
            newton.linearVelocity=vec2(0,newtonyspeed.linearVelocity.y)
                stuff1center.linearVelocity=vec2(-newtonxspeed,0)
        elseif xmobility==false and ymobility==false then
            newton.linearVelocity=vec2(0,0)
            stuff1center.linearVelocity=vec2(-newtonxspeed,-newtonyspeed.linearVelocity.y)
        end
    end
    
    function Game:touched(touch)
        if touch.state==BEGAN then
            touching=true
            glide=true
        end
        if touch.state==ENDED then
            glide=false
        end
    end
    
    function collide(c)
        if c.state==BEGAN then
            if c.bodyA.info==main then
                newtonyspeed.linearVelocity=vec2(0,HEIGHT/4.096)
                print(c.bodyB.info)
                print(c.bodyB.x)
                if c.bodyB.info=="how is this possibly colliding with you?" then
                    newtonyspeed.x=-2
                end
            elseif c.bodyB.info==main then
                newtonyspeed.linearVelocity=vec2(0,HEIGHT/4.096)
            end
        end
    end
    

    Disclaimer: I try to be as independent from the forums as possible. If I am posting a bug here, it is because I've exhausted all other problems. I will not post my problems here before looking into them myself (as I have done in the past).

Sign In or Register to comment.