Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
List of Questions — Gideros Forum

List of Questions

twisttaptwisttap Member
edited July 2012 in General questions
Okay, attached is a basic player movement template I am trying to make. It has a scene (a physics bounding box), a box and 2 arrows. While working on this I want to ask questions about the problems I came by.

1- I saw in some examples that physics world iterations done inside the moving object self ( in Jumping game template the physics world iterations done inside the player itself) that may be okay for 1 object maybe, but what if I have 30 objects on stage. Isn't it good idea to have a general physics iteration that goes over all objects ?

2- In the file I attached, I run the iterations on main.lua. The Players physical body works fine but the Graphical representation to the player body is not attached to physical body, whats the fix for this ?

3- Also in the jumping template I saw that Player controls are embedded inside the again Player.lua, however I want to
make control pads as an another class and Player independent. And even tho I tried something like that, I can not message between Player Class <-> Buttons Class which both added to main.lua during runtime.

4-Also I can not find enough examples for physical velocity and force usage. (Is there a better way to search Gideros Forums ?)

5-I think it might be a good idea to demonstrate an advanced example of OOP basics like messaging between different Objects, inheritance, encapsulation etc.

6-I saw some suggestions on Forum that making a community game all together. I think before stepping to that, maybe we should all together go over the general documentation of Gideros API. We can decide on a standard for API and make advanced and better explanations with examples for the API or we can update the Ultimate Guide !

Thank you very much for your time.
zip
zip
Player.zip
19K

Likes: NunoPT

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

Comments

  • twisttaptwisttap Member
    edited July 2012
    Updated, Actually they attach if I comment out the world:step function or I subtract the positioning values of the Player sprite OR I add world:step inside the Player Class.

    Also for the code below when I declare body as a local variable then try to attach, it does not work. But when I do it as self.body it works, I think both should work here because both of them in the same class and in the same function.
    Player = Core.class(Sprite)
     
    function Player:init(startXPos, startYPos)
     
    	self.world = world
     
        --create ball bitmap object from ball graphic
        self.Player = Bitmap.new(Texture.new("player.png"))
        --reference center of the ball for positioning
        self.Player:setAnchorPoint(0.5,0.5)
     
    	self.Player:setPosition(startXPos,startYPos)
     
     
    	self.body = self.world:createBody{
    	type = b2.DYNAMIC_BODY, 	
    	position = {x=self.Player:getX(),y=self.Player:getY()},
    	angle = self.Player:getRotation() * math.pi /180,
    	fixedRotation=true,
    	}
     
    --	self.body:setPosition(self.Player:getX(), self.Player:getY())
    --  body:setAngle(self.Player:getRotation() * math.pi/180)
     
    	local poly = b2.PolygonShape.new();
     
    	poly:setAsBox(self.Player:getWidth()/2, self.Player:getHeight()/2)
     
    	local fixture = self.body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2}
     
    	self.Player.body = self.body
    	self.Player.body.type = "Player"
     
        self:addChild(self.Player)
     
        return self.Player
     
     
     
     
    end
    OnEnter stuff on main.lua
    function scene:onEnterFrame() 
        -- edit the step values if required. These are good defaults!
        world:step(1/60, 8, 3)
        --iterate through all child sprites
        for i = 1, self:getNumChildren() do
     
            --get specific sprite
            local sprite = self:getChildAt(i)
            -- check if sprite HAS a body (ie, physical object reference we added)
            if sprite.body then
                --update position to match box2d world object's position
                --get physical body reference
                local body = sprite.body
                --get body coordinates
                local bodyX, bodyY = body:getPosition()
                --apply coordinates to sprite
                sprite:setPosition(bodyX-200, bodyY-100)
                --apply rotation to sprite
                sprite:setRotation(body:getAngle() * 180 / math.pi)
            end
        end
    end
  • ar2rsawseenar2rsawseen Maintainer
    Hello, :)
    So I'll try explain

    1. Yes it is probably the best to have global iteration in game logic class, that handles many physical objects.

    2. So you have a Sprite object called scene and inside onEnterFrame event you take all it's children and iterate through them. If child has a body property, then it's a reference to physical body and we use it.
    But let's look at Player class. Player class itself is a Sprite object. Then you create a Bitmap object (which has body property with physical body) and add it to the player sprite. Then in Player class constructor you try to return that Bitmap object, but since it is a constructor all it returns is the instance of Player class, (probably overriding anything you try to return). So inside scene class you receive Sprite object which have Bitmap object with body as a child. And you add this Player sprite object to scene. So Bitmap object with physical body is not on the first level of all children.
    When you iterate through scene children, all you get is a Player Sprite object, which does not have body property, but as a child has Bitmap object. (Hopefully it makes sense)

    3. Well in my point of view, you have all those different classes as Player, controls, etc and one main game logic class, which is also a scene, where you handle all this objects.
    And as suggestion, you can try using GregBug's http://www.tntparticlesengine.com/?cat=13

    4. Better forum search, might be site:giderosmobile.com/forum box2d in google
    But you can also check here for examples: http://appcodingeasy.com/Gideros-Mobile
    Not specifically force and impulses, but they are used as to demonstrate different combinations of effects, as for example, magnets

  • @ar2sawseen Okay then what do you suggest for question number 2 ? Reaching the physical bodies from main.lua file ? And yea I can use teh game template, the Control pad and all the stuff but then it comes to a point that I learn nothing about Gideros :)
    thank you
  • @twisttap that is certainly what I like to hear, someone that WANTS to learn the basics before uses the amazing packages that are being offered--and they are pretty amazing really :)

    I would suggest you don't name local variables the same as your classes or you may be running into a lot of trouble due to your using a local variable named Player inside a class named Player, which means you are liking either overwriting the data or at the least misreferencing things.

    @ar2rsawseen is of course right here, you should go ahead and assign the body to your Player Obj, instead of directly to the child sprite, so when the loop goes through it can access the body and move the whole player object--and it's child the sprite.
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • ar2rsawseenar2rsawseen Maintainer
    edited July 2012
    What I would suggest is to attach, bitmap object to Player class instance as child and set body property as property of Player class. Meaning inside scene onEnterFrame loop it will move Player's class instance as physical object, but since Bitmap is a child of this instance, it will also move along. :)

    Some thing like this:
    Player = Core.class(Sprite)
     
    function Player:init(startXPos, startYPos)
     
    	self.world = world
     
    	create player graphic
    	local playerImg = Bitmap.new(Texture.new("player.png"))
     
    	--add it to player's sprite as a child
    	self:addChild(playerImg)
     
    	--set anchor point to graphic
    	playerImg:setAnchorPoint(0.5,0.5)
     
    	--set graphic's position to 0
    	playerImg:setPosition(0,0)
     
    	--but sprite's position to needed coordinates
    	self:setPosition(startXPos,startYPos)
     
    	--create box2d physical object
    	local body = self.world:createBody{type = b2.DYNAMIC_BODY, fixedRotation=true}
    	--get coordinates from sprite
    	body:setPosition(self:getX(), self:getY())
    	body:setAngle(self:getRotation() * math.pi/180)
    	local poly = b2.PolygonShape.new();
    	poly:setAsBox(self:getWidth()/2, self:getHeight()/2)
     
    	local fixture = body:createFixture{shape = poly, density = 50.0, friction = 0.1, restitution = 0.2}
     
    	--and set property as usual
    	self.body = body
    	self.body.type = "Player"
    end
    And yea I can use teh game template, the Control pad and all the stuff but then it comes to a point that I learn nothing about Gideros :)
    thank you
    Believe me I know the feeling. I'm a fan of reinventing the wheel myself. That is also how I learn :)
  • @Teranth or @ar2rsawseen could you please more specific by attaching body to player obj? I think I also did that but still the physical body and Bitmap does not stay top of each other. Physical body is always away from Bitmap. If I create Player with Player.new(100,100) then if Physical body is at 0,0 bitmap spawns at 100,100.

    Btw I have an another idea which I didn't tried yet but, can I make a function inside all the assets I have and inside the main loop I will make just one world step call and inside that call other Objects update functions ? Is that useable ? I bought at least I will make just 1 onEnterframe event which seems cool
    Player = Core.class(Sprite)
     
    function Player:init(startXPos, startYPos)
     
    	self.world = world
     
        --create ball bitmap object from ball graphic
        self.PlayerBitmap = Bitmap.new(Texture.new("images/player.png"))
        --reference center of the ball for positioning
        self.PlayerBitmap:setAnchorPoint(0.5,0.5)
     
    	self.PlayerBitmap:setPosition(startXPos,startYPos)
     
     
    	self.Bbody = self.world:createBody{
    	type = b2.DYNAMIC_BODY, 	
    	position = {x=self.PlayerBitmap:getX(),y=self.PlayerBitmap:getY()},
    	angle = self.PlayerBitmap:getRotation() * math.pi /180,
    	fixedRotation=true,
    	}
     
    --	self.body:setPosition(self.Player:getX(), self.Player:getY())
    --  body:setAngle(self.Player:getRotation() * math.pi/180)
     
    	local poly = b2.PolygonShape.new();
     
    	poly:setAsBox(self.PlayerBitmap:getWidth()/2, self.PlayerBitmap:getHeight()/2)
     
    	local fixture = self.Bbody:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2}
     
    	self.body = self.Bbody
    	self.body.type = "Player"
     
        self:addChild(self.PlayerBitmap)
        --self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
        return self.Player
     
     
     
     
    end
     
     
    function Player:move(dir)
     
    print(dir)
    Player.body:applyForce(500 * dir, 0, 0.5,0.5)
     
    end
  • @ar2rsawseen you posted 1 second before me, if I saw that I'd not posted :)
  • ar2rsawseenar2rsawseen Maintainer
    edited July 2012
    :)
    But I hope now you understand what I meant and most importantly why it works.
    Creating an update position function for each object also solves the problem. But then you must maintain that every new object will have that method, or check if that method exists.

    You can go either way, it's totally up to you, lua is pretty flexible :)
  • What I am thinking is making a actor class that includes all the methods that a player/npc etc needs then extend from that class specific object like player, enemy, etc etc.
    And thanks I got the idea, actually I got your idea before but I just can't find enough documentation :/ There are lots of examples and stuff over here and giderostutorials.com for very cool subjects but I think in my humble opinion some nuances are missing that make big difference.
  • twisttaptwisttap Member
    edited July 2012
    thank you :)
Sign In or Register to comment.