Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
What's the right way to move a BODY ? — Gideros Forum

What's the right way to move a BODY ?

RogerTRogerT Member
edited February 2013 in General questions
Hi guys,
Say I have a world with no gravity and have a body, that I want to move in a direction it is facing.

So far I used

local ang = body:getAngle()
local x, y = body:getPosition()

x = x - math.sin(ang)*speed
y = y + math.cos(ang)*speed

body:setPosition(x,y)

on every frame. Yet it appears that setPosition is not the best solution when it bumps into other objects, it tries to go through them and look rather unnatural.

Please suggest, what method should I use to move this body? some applyForce or something?

Thanks

Comments

  • ScouserScouser Guru
    edited February 2013 Accepted Answer
    Have you tried something like:
    local ang = body:getAngle()
     
    local vx = -math.sin(ang)*speed
    local vy = math.cos(ang)*speed
     
    body:setLinearVelocity(vx, vy)
    I'm not 100% sure but I think this is probably what you require :)

    Likes: RogerT

    +1 -1 (+1 / -0 )Share on Facebook
  • Yes, looks like setLinearVelocity is what I need. ) THanks
  • twisttaptwisttap Member
    edited February 2013 Accepted Answer
    @RogerT, @Scouser In my opinion both setPosition and setLinearVelocity are not good ways to move a body because in real world and with real physics, it is impossible to move a body in a way like those two functions do.
    I think using impulses or applyForce works best and seems more real.
    Below is something I use with TNTpad
     
    function main:leftJoy(e)
    local actor = assets.Actors["Player"].body   -- Get actor body from asset pool
    local xVel, yVel = actor:getLinearVelocity()  -- Get Current Velocity
    local force = 0
    local desiredSpeed = 0
    	if e.data.power > 0.7 then
     
    			local direction = 1
    			if (math.cos(e.data.angle) < 0) then
    				desiredSpeed = self:speedMax(actor, -1, -10)			
    			else
    				desiredSpeed = self:speedMax(actor, 1, 10)
    			end
     
     
    	elseif e.data.power == 0 then
    		desiredSpeed = 0
    	else
    		desiredSpeed = 0
    	end
     
    local velchange = desiredSpeed - xVel
    force = actor:getMass() * velchange / (1/60)
    actor:applyForce(force, 0 , actor:getWorldCenter() )
     
    end
     
    -- This is a function to increase bodys speed to the maxValue in the specified steps.
    function main:speedMax(_body, _step, _maxValue)
    	local velx, vely = _body:getLinearVelocity()
    	if (math.abs(velx) < math.abs(_maxValue)) then
    		velx = velx + _step	
    	end
    	return velx
    end
    Cheers :)


    Likes: RogerT

    +1 -1 (+1 / -0 )Share on Facebook
  • Damm all these sensible answers, I was going to make some glib comment about waiting till it's really dark and then getting two trustworthy friends, a large piece of canvas, a pickup truck and some good shovels, but it's too late now :(

    :))
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+4 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013
    agree with @twisttap I use setLinearVelocity only when I have a moving body and I need to destroy for some reason (for example to change a shape or size) so I get velocity (angular and linear) and then set it to a new body so it would continue to move as if nothing happened ;)

    @techdojo don't worry, if I had to move a body, I'd do it your way! ;)
  • There are some important things to consider. 1. What kind of body is it? Dynamic, kinematic , static. They have different properties and some of them are not simulated other are partial and some are fully simulated. If you set position of dynamic body with body:setPosition() you override all the simulations made by box2d. According to box2d docs the best way to move dynamic bodies is to use applyLinearVelocity or applyLinearImpulse both give natural looking motion. Kinematic bodies are moved by setting their velocity. Dynamic by the forces and static only by their coordinates.

    Likes: chipster123

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.