Howdy, Stranger!

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

Help.Space shooter angle and direction

edited August 2013 in Questions Posts: 10

Hey, ive been making a space shooter and want to make enemys shoot at the players position. Ive worked out the angle the enemy should shoot at but im not sure how to change the direction accordingly, or wether this is the right way to do this. Any ideas? Heres my example:

function setup()

    parameter.watch("angle")
    ship = vec2(400,200)
    enemy = vec2(400,600)
    b = {}
    for i = 1,#b do
        b[i] = Bullet(x,y,a)
    end
end

function draw()

    background()

    if CurrentTouch.x > ship.x then ship.x = ship.x + 5 end
    if CurrentTouch.x < ship.x then ship.x = ship.x - 5 end

    sprite("Space Art:Red Ship",ship.x,ship.y)

    opp = ship.x - enemy.x -- x distance between enemy and ship
    adj = enemy.y - ship.y -- y distance between enemy and ship
    angle = math.deg(math.atan(opp/adj)) -- angle between opposite and hypotenuse

    sprite("Space Art:Green Ship",enemy.x,enemy.y)

    for i = 1,#b do
        b[i]:draw()
    end
end

function touched(touch)

    if CurrentTouch.state == ENDED then
        table.insert(b,Bullet(enemy.x,enemy.y,angle))
    end
end

Bullet = class()

function Bullet:init(x,y,a)

    self.x = x
    self.y = y
    self.angle = a
end

function Bullet:draw()

    pushMatrix()
    translate(self.x,self.y)
    self.x = self.x + 0
    self.y = self.y - 5
    rotate(self.angle)
    sprite("Tyrian Remastered:Bullet Streak",0,0)
    popMatrix()
end

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 8,192

    @JeffTheNoob I posted this for someone else, I don't remember who, but maybe this will help. Slide your thumb on the right side to rotate the ship and tap the left side to fire missiles.


    function setup()     displayMode(FULLSCREEN)     tab1={}     m=0     b1={}     x=1     ang=0     new =0     old=0     xa=0     ya=1     yy=20     targets() end function draw()     local x,y,xx     background(40, 40, 50)     fill(255,0,0)     pushMatrix()     translate(WIDTH/2,HEIGHT/2)     rotate(180-ang)     sprite("Tyrian Remastered:Plane Boss Destroyed",0,0)     popMatrix()          xx=0     for z=1,5 do         x=tab1[z].x         y=tab1[z].y         ellipse(x,y,20,20)         if x>0 and x<WIDTH and y>0 and y<HEIGHT then             xx=1          end         end     if xx==0 then         targets()     end          xx=0     for z=1,m do         x=b1[z].x         y=b1[z].y         fill(255,0,0)         ellipse(x,y,5,5)         if x>0 and x<WIDTH and y>0 and y<HEIGHT then             xx=1         end     end     if xx==0 then         m=0     end  end function shoot(x)     b1[x] = physics.body(CIRCLE,5)     b1[x].x=WIDTH/2     b1[x].y=HEIGHT/2     b1[x].gravityScale=0     b1[x].linearVelocity=vec2(xa*400,ya*400)  end function targets()     local x     for x=1,5 do         tab1[x]=physics.body(CIRCLE,10)         tab1[x].x=math.random(WIDTH)         tab1[x].y=math.random(HEIGHT)         tab1[x].gravityScale=0     end end        function touched(t)     if t.state == BEGAN then         if CurrentTouch.x < WIDTH/2 then             m = m + 1                     shoot(m)         end     end     if CurrentTouch.x>WIDTH/2 then         ang=ang+t.deltaY         xa=math.sin(math.rad(ang))         ya=math.cos(math.rad(ang))      end            end
  • @dave1707 yeah that helped thanks, was the xa ya bit i needed, heres the edited code

    function setup()
        parameter.watch("angle")
        ship = vec2(400,200)
        enemy = vec2(400,500)
        b = {}
        for i = 1,#b do b[i] = Bullet(x,y,a) end
        count = 0
        c = CurrentTouch
    end
    
    function draw()
        background()
    
        angle = math.deg(math.atan((ship.x - enemy.x)/(enemy.y - ship.y)))
    
        if ship.y > enemy.y then angle = angle + 180 end
    
        sprite("Space Art:Red Ship",ship.x,ship.y)
    
        for i = 1,#b do b[i]:draw() end
    
        sprite("Space Art:Green Ship",enemy.x,enemy.y)
    
        count = count + 1
        if count >= 5 then count = 0 end
        if count == 0 then table.insert(b,Bullet(enemy.x,enemy.y,angle)) end
    end
    
    function touched(touch)
        if c.state == ENDED then
        else
            ship.x = ship.x + c.deltaX
            ship.y = ship.y + c.deltaY
        end
    end
    
    Bullet = class()
    
    function Bullet:init(x,y,a)
        self.x = x
        self.y = y
        self.angle = a
        self.xa = math.sin(math.rad(self.angle))
        self.ya = math.cos(math.rad(self.angle)) 
    end
    
    function Bullet:draw()
        pushMatrix()
        self.x = self.x + self.xa * 10
        self.y = self.y - self.ya * 10
        translate(self.x,self.y)
        rotate(self.angle)
        sprite("Tyrian Remastered:Bullet Streak",0,0)
        popMatrix()
    end
    
  • dave1707dave1707 Mod
    edited September 2013 Posts: 8,192

    @JeffTheNoob Glad that helped. I made a minor change to rotate the green ship in your code in the draw function. I put a "new code" comment above it.


    function setup()     parameter.watch("angle")     ship = vec2(400,200)     enemy = vec2(400,500)     b = {}     for i = 1,#b do b[i] = Bullet(x,y,a) end     count = 0     c = CurrentTouch end function draw()     background()     angle = math.deg(math.atan((ship.x - enemy.x)/(enemy.y - ship.y)))     if ship.y > enemy.y then         angle = angle + 180      end          -- new code     pushMatrix()     translate(enemy.x,enemy.y)     rotate(180+angle)     sprite("Space Art:Green Ship",0,0)     popMatrix()               sprite("Space Art:Red Ship",ship.x,ship.y)     for i = 1,#b do          b[i]:draw()      end     count = count + 1     if count >= 5 then          count = 0      end     if count == 0 then          table.insert(b,Bullet(enemy.x,enemy.y,angle))      end end function touched(touch)     if c.state == ENDED then     else         ship.x = ship.x + c.deltaX         ship.y = ship.y + c.deltaY     end end Bullet = class() function Bullet:init(x,y,a)     self.x = x     self.y = y     self.angle = a     self.xa = math.sin(math.rad(self.angle))     self.ya = math.cos(math.rad(self.angle))  end function Bullet:draw()     pushMatrix()     self.x = self.x + self.xa * 10     self.y = self.y - self.ya * 10     translate(self.x,self.y)     rotate(self.angle)     sprite("Tyrian Remastered:Bullet Streak",0,0)     popMatrix() end
Sign In or Register to comment.