Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to make a body jump while moving in an ellipse — Gideros Forum

How to make a body jump while moving in an ellipse

simransimran Member
edited May 2014 in General questions
I have given piece of code for a game that is to be played in landscape left mode. first piece of code is executed as soon as the player enters the frame and this results in motion of player along an ellipse. the code inside first else is executed when user touches the screen and at this moment I want the player to jump, and code inside second jump is executed when touch ends that is player gets back to initial position, please help me what shall be the code to make the player jump while in elliptical motion

if playertouch == false then
if playertouchend == false then
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
else
--code to jump here
end
else
--code to get back down

end

Likes: Yan

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • simransimran Member
    To, jump, I need to use Z axis , right? or is it possible to do it alone using angle?, please? and it's possible only with z axis, how to do that, since setPosition allows only x, y psitions to set, help :/
  • amin13aamin13a Member
    Hi simran
    Try this Code:(just copy it to main.lua)
    -- You can jump by holding touch and back by release! (Sorry for my English :D)
    application:setOrientation("landscapeLeft") -- good in 480*800


    local xc = 400 -- X center of Ellipse
    local yc = 200 -- Y center of Ellipse
    local a = 400 -- big radius
    local b = 100 -- small radius
    local speed = 0.5 -- speed of rotating
    local jump = 0
    local maxjump = 50 -- can jump THIS pixel above
    local jumpspeed = 1 -- speed of jumping

    local theta = 0
    local degree = math.pi / 180
    local sinus = math.sin
    local cosinus = math.cos

    local sprite = gideros.class(Sprite)
    function sprite:init()
    self.jump = false
    local mesh = Mesh.new()
    mesh:setVertex(1,0,0)
    mesh:setVertex(2,10,0)
    mesh:setVertex(3,10,30)
    mesh:setVertex(4,0,30)
    mesh:setIndices(1,1,2,2,3,3,4,1,5,3,6,4)
    mesh:setColorTransform(0.9,0,0,1)
    self:addChild(mesh)
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame,self)
    self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)


    end
    function sprite:onEnterFrame()
    if theta > 3600 then
    theta = 3600 - theta
    end
    if self.jump and jump < maxjump then
    jump = jump + jumpspeed
    elseif jump > 0 then
    jump = jump - jumpspeed
    end

    local angle = theta*degree
    local x = xc + a * cosinus(angle)
    local y = yc + b * sinus(angle) - jump
    self:setPosition(x,y)
    theta = theta + speed
    end
    function sprite:onTouchesBegin()
    self.jump = true
    end
    function sprite:onTouchesEnd()
    self.jump = false
    end

    mysprite = sprite.new()
    stage:addChild(mysprite)



  • simransimran Member
    no, it did not work for me :(
  • amin13aamin13a Member
    what is exactly your problem? (don't be sad :D)
  • amin13aamin13a Member
    Gideros is 2D gameEngine and there is no z axis here.
    your body can jump in y axis (like most platform games)
  • simransimran Member
    edited May 2014
    @amin13a : Okay, no z axis, but I need a combination of x and y movement while it jumps,
    this is what I am using for its motion in ellipse

    x = 215 + math.cos(angle)*195
    y = 130 + math.sin(angle)*115

    Now, what shall i do to make the body jump while it is in motion along the ellipse, I want it to jump in the constraints of the ellipse.
    Any idea, please?

  • amin13aamin13a Member
    if jump means going further from center of ellipse (but remain on the elliptical path)
    you can change my code by this order:

    local jump = 1
    local maxjump = 1.2 -- can jump THIS pixel above
    local jumpspeed = 0.02 -- speed of jumping



    local x = xc + a * cosinus(angle)* jump
    local y = yc + b * sinus(angle) *jump
  • amin13aamin13a Member
    Edit:
    elseif jump > 0 then
    must be changed by:
    elseif jump > 1 then
  • simransimran Member
    Following code worked, Thanks for your help ;D

    if playertouch then
    speed = 10
    angle2 = 30
    speedx = math.cos(20)
    speedy = math.sin(130)
    velocityx = speed*speedx
    velocityy = speed*speedy
    for i = 1, 1 do
    x = x + velocityx
    y = y + velocityy
    self: setScaleY(1.8)
    end

    elseif playertouch == false then
    for i = 1,1 do
    x = x - velocityx
    y = y - velocityy
    x = 215 + math.cos(angle)*195
    y = 130 + math.sin(angle)*115
    self:setScaleY(1)
    end
    else
    x = 215 + math.cos(angle)*195
    y = 130 + math.sin(angle)*115
    end
Sign In or Register to comment.