Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
box2d object with spring - Page 2 — Gideros Forum

box2d object with spring

2

Comments

  • Hi...
    Thank you..

    How do I calculate LinearVelocity or LinearImpulse to throw a pumpkin.

    In my game I've set the stick to 25 degrees inclined... The pumpkin is placed on the top of the stick. I can drag the stick from top to down to between 25 to -25 degrees.

    So when the mouse is up then I want to throw pumpkin based on the angle dragged.
    What should I apply?

    Either
              applyLinearImpulse()
    or
              applyForce()
    or
              setLinearVelocity()
    How do I calculate the parameters for the above functions...
    The stick is not a Bitmap image, Its not a dynamic body...

    Currently I've written code on onMouseMove event as follow:
                    local a = math.atan((event.y - self.pumpkinStick:getY()) / (event.x -              self.pumpkinStick:getX())) * 180 / math.pi
    		if math.ceil(a) <= -25 then
    			self.pumpkinStick:setRotation(-25)
    			self.pumpkinStick:setVisible(true)
    			self.stick:setVisible(false)
    		elseif math.ceil(a) >= 25 then
    			self.pumpkinStick:setRotation(25)
    			self.pumpkinStick:setVisible(true)
    			self.stick:setVisible(false)
    		else
    			self.pumpkinStick:setRotation(a)
    		end
    and on onMouseUp as follow:
                    local theta
    		if self.pumpkinStick:getRotation() > 0 then
    			theta = 50 - self.pumpkinStick:getRotation()
    		elseif self.pumpkinStick:getRotation() < 0 then
    			theta = 25 - (-1 * self.pumpkinStick:getRotation())
    		else
    			theta = 12
    		end
     
                    xImpulse = ((25 - theta) / 25) * 8
                    yImpulse = (-1 * xImpulse) - 2
    		self.pumpkinStick:setRotation(25)
    		if xImpulse < 4 and xImpulse ~= 0 then
    			xImpulse = 4
    		end
    		local x, y = self.pumpkin.body:getPosition()
    		self.pumpkin.body:applyLinearImpulse(xImpulse*50 , yImpulse*50, x, y)
    It works fine but there is no speed....
    Please help me...
  • The movement of the pumpkin is fine... Its forms correct arc, but only problem is the speed.... There is no speed in the movement....

    Hence when it hits the spring anywhere the reaction of the spring will be same all the time... The working of the spring doesn't look like real world..

    And there no bounce effect for pumpkin, restitution is set to 0.8 but still the pumpkin does not bounces when it hits any wall or spring...

    when I comment the onBeginContact event, then there will be bouncing in pumpkin..

    How do I fix it..?
  • @Sush19 if you are using the example I provide, then it resets all velocities on collision, so it won't move further.
    As you had the same effect in your project only by using negative density, I've implemented it in my example by reseting the velocity on collision.

    So
    1) check if density is not equals or lower than 0
    2) check if no velocities are changed on collision
  • The density of the pumpkin is 100 and the density of rest of the bodies is set to 1...
    None of the density is less than or equal to 0.

    There is no change in velocities on collision...
    I just wanted to apply little bouncing for pumpkin. i.e when it touches other bodies such as the boundary walls or spring then the pumpkin should bounce...
  • @Sush19 well such huge density as 100 might also prevent it from flying and bouncing, etc. Try decreasing it :)
  • Thank you.... I tried changing so many things, some how its improved..

    Thank you for your guide through..
    Will be back with if problem arises...
  • Hello...

    I'm moving the scene as you told me
            local screenW = application:getContentWidth()
    	local screenH = application:getContentHeight()
     
            local offsetX = 0
    	local offsetY = 0
     
            if((self.worldW - self.pumpkin:getX()) < screenW/2) then --self.worldW = screenW*10
    		offsetX = (-self.worldW + screenW)
    	elseif(self.pumpkin:getX() >= screenW/2) then
    		offsetX = -(self.pumpkin:getX() - screenW/2)
    	end
     
    	self:setX(offsetX)
     
    	if((self.worldH - self.pumpkin:getY()) < screenH/2) then --self.worldH = screenH*1.37
    		offsetY = -self.worldH + screenH 
    	elseif(self.pumpkin:getY()>= screenH/2) then
    		offsetY = -(self.pumpkin:getY() - screenH/2)
    	end
     
    	self:setY(offsetY)
     
            --and other backgrounds are based on below code, as you told me....
            --moving parallax backgrounds
            self.bg1:setX(-offsetX) --will move with the screen
            self.bg2:setX(-offsetX*0.8) --will move slower
            self.bg3:setX(-offsetX*0.4) --will move faster
            --and not moving self.bg4 as it will move with the scene
    This time I'm moving on Y-axis as well...
    I want to move the scene and backgrounds based on the speed of the pumpkin,
    Based on the velocity of the pumpkin I need to move the backgrounds..
    How do I implement it...
    :) :) :)
  • Hello ;)
    And does x axis moving work as expected?
  • Yes the x-axis and the y-axis moves as expected.... But when the pumpkin reaches the half of the screen then its speed gets slow down, I don't know exactly what is happening there,,, but I felt that the speed of the pumpkin before half of the screen is little more than when it reaches the half of the screen..

    For your reference I've Included my project here...
    In my project base wall is the fastest moving background and the background on the top moves along with the screen and the grass is the slowest background..

    Even the spring should move based on the base wall speed...

    Thank you... :)
    zip
    zip
    BumpyPumpkin.zip
    583K
  • ar2rsawseenar2rsawseen Maintainer
    edited September 2013
    Here is a test on my pc, it seems to be behaving quite normally without any speed decrease:
    http://www.screenr.com/XPGH

    ;)
  • But when the pumpkin reaches half of the screen, then it seems to be like pumpkin changes its y-axis direction, It seems to be like pumpkin goes little upwards in y-axis.., This we can make out when we fully drag down the stick..
    Which doesn't looks good and doesn't form a proper arc...

    Hope you understood what I mean to say...
  • How do I add and manage more that one spring in my above project?

    I tried adding more than one spring, but all the springs are not working fine...
    Only the last added spring works fine and all other springs doesn't work...

    How do I do this?
    I wanted to add some 3 to 4 spring on random x position....

    I've already added but doesn't work for me...

    Please help me..

  • Hello sir Please help me.... How can I add more than 1 spring in my game? The problem I'm facing is written in the above post..

    Please help me and reply me
    Thank you :)
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Updated example creating the createSpring method, with which to create multiple springs ;)
    zip
    zip
    BumpyPumpkinModified.zip
    575K
  • Thank you so much...
    It
  • Thank you so much...
    It works great...

    thanks a lot :) :) :)
  • Hi...
    I need another help..
    There is no bounce effect on pumpkin, I want pumpkin to bounce when it hits the walls, but because of the following event There is no bounce effect
    self.world:addEventListener(Event.BEGIN_CONTACT, self.onBeginContact, self);
    if I comment this statement then there is bounce effect...

    Right now the pumpkin stops wherever it falls on the bottom wall, I want the pumpkin to show little bounce and roll some distance based on its speed...

    What I should do to get the bounce effect...
    I've set:
            -- For pumpkin
            local fixture = body:createFixture{shape = circle, density = 10, friction = 10, restitution = 0.1}
    	fixture:setFilterData({categoryBits = 2, maskBits = 9})
     
            -- For walls
            local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.8}
    	fixture:setFilterData({categoryBits = 1, maskBits = 14})
  • Inside function scene:onBeginContact(e) it resets the pumpkin velocity

    what you need to do is to check, if pumpkin collides with spring only then reset, other wise do not reset and let it bounce ;)
  • Thank you...
    I tried the same and it works...
    :)
  • Hi,
    In my game when the pumpkin hits the edge of the springTop, then the springTop starts shaking to and fro, and it continues until reload the game...
    How do I avoid shaking of the springTop when a body hits at the edge of it..
    :)
  • hmm, maybe you can reset it's velocity (set to 0 both linear and angular velocities) when collision with pumpkin ends.

    Or experiment with restitution and friction of srpingTop
  • I tested it by reseting it's LinearVelocity and AngularVelocity to 0, but it didn't work...
    I'll try with restitution and friction now..
    Thank you..
  • I've set my game Orientation to Landscape as
            application:setOrientation(Application.LANDSCAPE_LEFT)
    And I'm showing textInputDialog and alertDialog as
            textInputDialog = TextInputDialog.new("title", "msg", "", "OK")
            textInputDialog:setInputType(TextInputDialog.NUMBER)
            textInputDialog:show()
    	textInputDialog:addEventListener(Event.COMPLETE, onComplete)
     
            alertDialog = AlertDialog.new("title", msg, "OK")
    	alertDialog:addEventListener(Event.COMPLETE, onComplete, n)
    	alertDialog:show()
    The text input dialog and alert dialog appears in portrait mode, I want it to display in landscape mode
    I tried with
            textInputDialog:setOrientation(textInputDialog.LANDSCAPE_LEFT)
            alertDialog:setOrientation(alertDialogg.LANDSCAPE_LEFT)
    Which throws error...

    How can I set it to display it in Landscape Mode..
  • @Sush19 if it is in Gideros Player, then Gideros player was built with portrait settings and there is nothing you can do that.

    If it is with exported project, then for Android you need to replace portrait with landscape in your AndroidManifest activity tag

    If IOS then you can set allowed orientation in General settings tab by checking/unchecking device icons in different orientations ;)
  • oh.. I got it...

    In my project there are 3 files
    main.lua
    game.lua
    gameEnd.lua

    I'm loading the initial sound, background Image and playButton image in main.lua
    When playButton is clicked I'm calling game.lua as
            for i = 1, stage:getNumChildren() do
    		stage:removeChildAt(1)
    	end
            stage:addChild(scene.new())
    and I'm loading all the assets inside game.lua, when the game is over I'm calling gameEnd.lua as
            for i = 1, stage:getNumChildren() do
    		stage:removeChildAt(1)
    	end
            stage:addChild(endGame.new())
    Inside gameEnd.lua I'm displaying the score and adding replay button, and on click of replayButton again I'm calling game.lua as
            for i = 1, stage:getNumChildren() do
    		stage:removeChildAt(1)
    	end
            stage:addChild(scene.new())
    When I load the game the game plays fine on device, but when I click replay each time the game get slower and slower on device...

    Whether this lines frees the memory as well?
            for i = 1, stage:getNumChildren() do
    		stage:removeChildAt(1)
    	end
    How to optimize the game...?
  • I'll try implement my game in GameTemplate...

    I want to add some points, say: coins or stars.
    I want to add N number of points in different positions
    When the pumpkin touches these stars then it should disappear from the stage without affecting the pumpkin movement and at the same time I want to play some sound and reward some extra points to the player.

    I've set
    pumpkin: categoryBit = 1 and maskBits = 26
    springTop: categoryBit = 2 and maskBits = 21
    springBound: categoryBit = 4 and maskBits = 18
    stars: categoryBit = 8 and maskBits = 17
    walls: categoryBit = 16 and maskBits = 15

    How do I achieve this?
    :)
  • About the points and stars you can check GameManager class inside GameTemplate.

    About colliding with stars, you also need to make star a sensor, by setting isSensor to true when creating fixture ;)
  • Hello @ar2rsawseen,

    As I told you regarding my game, that each time I replay without exiting the game, the game gets slower and slower...

    Now I'm using SceneManager as you suggested me.... But still there is the same problem..

    It works fine and it doesn't get slower when I replay the game on my PC...

    I created .apk and was trying on Android devices, on Android devices for the first time I play the game, that time it works fine, but when I replay without exiting the game, then each time I replay the game, the game gets slower and slower...

    I'm using 3 Scenes
    How do I improve it?
Sign In or Register to comment.