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

box2d object with spring

Sush19Sush19 Member
edited August 2013 in General questions
Hi once again...
Now I have a box2d object in my game, I wanted to add a Spring in my game and when a box2d object falls on the spring the spring should squeeze based on what force the object fall on it...

Can any one help me to reach to my this goal...

the sample of the spring is as follow....
and its a png image....image

Likes: jdbc

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

Comments

  • @Sush19 you could separate the top from the spring.
    Then create distance joint with dynamic object attached to the ground wall.
    More on joints:
    http://appcodingeasy.com/Gideros-Mobile/Using-box2d-joints-in-Gideros

    Then measure the distance between ground wall and dynamic object and scale the spring accordingly. This should give the effect of bouncing spring. :)
  • @ar2rsawseen
    Hello sir..
    As you told me to separate the top from the spring, I've created new images top and spring separately...

    I've created both as dynamic body i.e both top and spring as dynamic body as follows:

    in my init() function I'm calling
            local x = math.random(200, 500)
            self.spring = self:CreateSpring(x, 250, "images/Springs.png")
    	self.springTop = self:CreateSpring(x, 190, "images/SpringTOp.png")
            -- Creating Distance Joint
            local jointDef = b2.createDistanceJointDef(self.spring.body, self.springTop.body, x, 250, x, 250)
    	local distanceJoint = self.world:createJoint(jointDef)
    	distanceJoint:setLength(35)
     
    function LoadGame:CreateSpring(x, y, img)
    	spring = Bitmap.new(Texture.new(img));
    	spring:setAnchorPoint(0.5, 0.5)
    	spring:setPosition(x, y);
    	stage:addChild(spring);
     
    	local radius = spring:getWidth() / 2
     
    	local body = self.world:createBody{type = b2.DYNAMIC_BODY}
    	body:setPosition(spring:getX(), spring:getY())
    	body:setAngle(spring:getRotation() * math.pi/180)
    	local circle = b2.CircleShape.new(0, 0, radius)
    	body:createFixture{shape = circle, density = 0, friction = 5, restitution = 0.8}
    	spring.body = body
    	spring.body.type = "spring"
    	self:addChild(spring)
    	return spring
    end
    I don't want any mouse event on Spring so I've not registered any mouse event for spring, in fact when i throw pumpkin(box2d body) and if it falls on the spring top it must show normal spring behavior.

    Now when I throw the pumpkin(box2d body) and when it falls on the spring top, the top of the spring falls down... I'm not getting any idea what has to be done..

    Can me help me out to solve my problem..

    whether what I'm doing is correct?

    Please help me out.. First of all I'm new to gaming, and in that i'm working with lua, which is another new thing for me....

    Thank you.... :)
  • @Sush19 for a new it seems like you are doing quite great.
    Let me try to create a simple example ;)
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2013 Accepted Answer
    Here is a quick example attached to this post just to get you started.

    And here is the video of how it looks like:


    1) You will need to adjust frequency and dampeningRatio to the feel of the spring you want in your game
    2) There are side supports, that will not allow dynamic body of distance joint to drop to the right and the left side, but they also affect other dynamic bodies. If you do not want that and want these side supports to affect only spring body, you can use collision filtering for that.
    Here is a great tutorial on collision filters: http://www.giderosmobile.com/forum/discussion/1070/a-little-information-about-collision-filters/p1
    3) to animate your spring you do not need to actually add it to the body, but rather place it as a bitmap image with anchor point to the bottom and then inside ENTER_FRAME event check the distance between the ground and the spring top body and scale the bitmap accordingly, you should get nice elastic effect from that (if you can provide separate top and spring images I can show you what I mean)

    If you run into any more troubles, post here will try to solve that together, box2d stuff are so fun to play with :)
    zip
    zip
    SpringExample.zip
    28K

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • Hello Sir...

    Thank you so much for the example.... I'll try this now in my game...

    Here are two different images attached...

    :) :) :)
    Springs.png
    34 x 64 - 2K
    SpringTOp.png
    62 x 29 - 2K
  • I tried it in my game.. But I didn't get how to apply scaling to the bitmap..
    I've provided spring and top of the spring separately in previous post, If i get how it works, then it will be really thankful...

    waiting from your end... :)
  • Sure, here is attached project and example how it works in the video
    zip
    zip
    SpringExample.zip
    33K

    Likes: fxone, MoKaLux

    +1 -1 (+2 / -0 )Share on Facebook
  • Hi...

    I tried the above code and its working fine....
    Thanks alot... :) :) :)

    Please don't mind, I'll be asking so many questions....

    My next question is:

    When i throw a pumpkin from a point and when it falls on the spring (which i created as above) the pumpkin does not bounce, in facts it slides and falls down...

    I think because it of pumpkin weight..

    How to set the mass of the body..

    Or any other solutions will help..

    Thank you :)
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2013
    The weight depends on the area of pumpkin (its size) and density you provide when creating fixture
    You can also try increasing friction of the pumpkin and spring also when creating fixture.

    also playing with frequency of the spring might help.

    It would be cool to see a video, if possible, that would help provide more suggestions :)

  • Hi..
    I'll try to take a video of my game..
    It is very simple game, In fact it is a educational game for small kid..

    It is a mathematical game, i.e to teach kids about numbers and rounding off...

    The concept of my game is to throw a pumpkin from a place and when it falls on the ground, (there is a scale on the ground starting from 0, 0.5 and so on...) the kid have to enter the distance covered by the pumpkin..

    but its not that much smooth...

    I want to improve my game and deploy it as soon as possible on iOS and Android...

    Please help me out for this..

    thank you for all your support.. :)
  • @Sush19 you can easily record the video using http://www.screenr.com/ from your pc, that is how I do it :)

    So if you already know where you want to throw the pumpkin before throwing it, you could attract pumpkin to specific spring like this:
    http://appcodingeasy.com/Gideros-Mobile/Gideros--Box2d-magnets

    Just an idea :)
  • Hi
    @ar2rsawseen
    I'm struggling to complete my game..
    Please help me out to improve and to complete my game..
    I was unable to record video of my game due to my PC problem...
    So I've attached my project here..

    Please go through it and help me out to improve it...

    My code may look childish..
    And what should be the width and height of the game to support in different devices..
    My game is for Landscape mode..

    Thank you....
    zip
    zip
    BumpyPumpkin.zip
    1M
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Hello, try this one.
    Basically what I did was increasing pumkin density to gain weight,
    and increasing spring's frequency for bounciness,
    then reseting pumkin velocity on collision so it won't go further
    and applying incoming velocity reflected by the x axis on collision with spring, so spring will throw it further (you may add coefficients there, to look to your need)

    There were also a lot of your code in enterframe event which for some reason made pumkin roll back, so I just commented it out for now, so you can figure out the issue, cause I have no idea of purpose of that code ;)

    zip
    zip
    BumpyPumpkinModified.zip
    1M
  • Hello....:)
    Thank you for your response...
    The code in enterframe event is to check the position of the pumpkin when it is moving, i.e whether it touches the bottom ground and its linear velocity is 0, So that i can show TextInputDialog to enter the distance covered by the pumpkin...

    And it also contains code to move all the background images from right to left when the pumpkin is thrown and it crossed half of the screen, and at the same time moving the pumpkin as well from right to left so that it always fall on the middle of the screen, and it seems to be moving long distance... Just like angry bird game effect..

    Is there any other way to do this? without writing code in enterframe event....
    :)
  • Oh it's completely ok to have, just make sure it does not change linear velocity of the pumpkin (does not make it roll back) ;)
  • Okay.... Thank you..

    Is there any problem with Gideros Player?

    When i run this game in my Windows laptop the pumpkin travels very slow,

    And same game when i run it in my MAC System, then it travels little faster than Win-Laptop

    I want the pumpkin to travel with speed, so what should I do to solve this problem...

    Whether it is a Emulator problem? Whether the game will be played fine in iPhone?
    :) :)
  • @Sush19 I've tested it on Windows and it worked great. There was once a similar issue with Windows machine in forum, and there were outdated video drivers, maybe that is the case?

    Either way it should run fine on the phones ;)
  • Can I test the game on my iPhone by connecting to my PC using data cable, so that I can make out the bugs and all... So that I can improve the performance of my game...?

    Just now I saw a Video:
    Gideros live coding with ZeroBrane Studio

    but we need to create some extra lua files for that...
    I don't wanna do live coding but instead I wanna check my game on iPhone/iPad or any android device in between by connecting my device using data cable.

    How can I do this? :-?
  • You need to install Gideros Player to your device and then you will be able to run it in the same LAN

    http://docs.giderosmobile.com/deployment.html
  • Hi, Thank you for your response.. :)

    I was trying to test my game on actual device but I was getting some error..
    So I just created a video of the game which I've included here..

    You can see that there is no smoothness in the movement.. its too slow and at the duration of 1:10 you can see the pumpkin bounces thrice then it is thrown from the spring...
    and even when the pumpkin hits the edge of the spring its starts rotating...

    So how do i fix all these problems...
  • I wanted some bounce effect as well, i.e when the pumpkin hits the walls, for that I tried to set restitution of the pumpkin and wall but I didn't get the bounce effect...
  • Hi!

    Here I've one more problem to solve it..
    When I throw my pumpkin then I'm scrolling the all background images from right to left by simply setting the X position of the each image, i.e imagename.setX(imagename.get() - 1) something like this...

    I want to do same thing to my pumpkin so that it never touches the right wall and it should always falls at the middle of the screen but it must look like its moving forward..

    I tried setting its X position but it doesn't work, as it is a box2d body...

    How can I fix this problem...
    If I apply force then the pumpkin roll's back when it touches the spring...

    Please help me... :)
  • in this case you should not move your background image, but move the scene itself instead of background image.
    Something like this:
    http://appcodingeasy.com/Gideros-Mobile/Gideros-Camera-Move
  • I went through this example...

    I've one background image, which is static, I'm not moving the main background image,
    I've more than 1 images placed on background such as road, housed, mountains etc and I want to move all these images with different speed...

    If I move the entire scene then all the Images will move at same speed...

    I hope you will understand my problem, how exactly I want...

    Just take a example of angry bird game, in which there are more than 1 background images such as mountains and all, but each moves at different speed, and when the angry bird is thrown it falls on the middle of the screen always or between some range but it seems to be moving forward..

    This we can make out when we put some mark on the screen while playing the game and when the angry bird falls down, we will notice that every time it falls between some range..

    I want something like this...
  • @Sush19 yes in this case, you need to select the slowest background and not move it (because it will move with the scene) and move faster backgrounds relatively to slowest, to regain parallax effect (basically move them as you did before, but with much much slower speed)
  • thank you... Will try it..
  • Hi again....

    I tried to implement it, but its not working fine for me...
    I referred http://appcodingeasy.com/Gideros-Mobile/Gideros-Camera-Move demo
    but I'm unable to do it properly in my game..

    I just want to check for right wall so, I've added only below code
            --get screen dimensions
    	local screenW = application:getContentWidth()
    	local screenH = application:getContentHeight()
     
    	--define offsets
    	local offsetX = 0
     
    	--check if we are not too close to left or right wall
    	--so we won't go further that wall
    	if((screenW*2 - self.pumpkin:getX()) < screenW/2) then
    		offsetX = -screenW*2 + screenW 
    	elseif(self.pumpkin:getX() >= screenW/2) then
    		offsetX = -(self.pumpkin:getX() - screenW/2)
    	end
     
    	--apply offset so scene
    	self:setX(offsetX)
    but the movement speed is very fast. How to control the speed, and how to move the background Bitmap along with scene with same speed?
  • 1) if background is attached to the scene, it should move with it without you doing anything additional.
    2) for second/third/etc parallax backgrounds you can take scene position, invert it and apply some kind of coefficient for proper speed, for example:
    self.bg2:setX(-offsetX*0.9)
    self.bg3:setX(-offsetX*0.8)
  • How do I attach background to the scene...?
    Is it defining with self key word?
    eg:
            self.bg = Bitmap.new(Texture.new("images/Background.png"));
    	self.bg:setPosition(0,0)
    	stage:addChild(self.bg)
    Is the above code correct to attach any image to the scene?

    In my case as you know there is one spring attached to the bottom wall of the scene, so when I move the scene it also moves along with it....

    There are 4 backgrounds
    Background1 with the slowest which should move along with scene.
    Followed by Background2 little faster than the Background1
    Followed by Background3 faster than the Background2
    Last Background the is fastest and the spring should move with the speed of this background...

    How do I achieve this?

    Thank you for your all time support... :) :) :)
  • so it should be something like this:
    creating backgrounds
    self.bg = Bitmap.new(Texture.new("images/Background1.png"));
    self:addChild(self.bg)
    self.bg2 = Bitmap.new(Texture.new("images/Background2.png"));
    self:addChild(self.bg2)
    self.bg3 = Bitmap.new(Texture.new("images/Background3.png"));
    self:addChild(self.bg3)
    self.bg4 = Bitmap.new(Texture.new("images/Background4.png"));
    self:addChild(self.bg4)
    and then inside onEnterFrame event:
    --get screen dimensions
    local screenW = application:getContentWidth()
    local screenH = application:getContentHeight()
     
    --define offsets
    local offsetX = 0
     
    --check if we are not too close to left or right wall
    --so we won't go further that wall
    if((screenW*2 - self.pumpkin:getX()) < screenW/2) then
    	offsetX = -screenW*2 + screenW 
    elseif(self.pumpkin:getX() >= screenW/2) then
    	offsetX = -(self.pumpkin:getX() - screenW/2)
    end
     
    --apply offset so scene
    self:setX(offsetX)
     
    --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
    ;)
Sign In or Register to comment.