Page 1 of 1

Having a problem

Posted: Wed Sep 10, 2008 5:52 pm
by Kuromeku
When I do:

self:setVelocity( self:getVelocity() + envy.vector:new(16, 0) );

it doesn't apply gravity still, so the ball just moves to the right, I'd like it to still apply gravity.

and before you say, I've tried: self:setVelocity( self:getVelocity() + envy.vector:new(16, 16) ); or something similiar and it turns out really shitty.

Re: Having a problem

Posted: Wed Sep 10, 2008 7:40 pm
by Kaze
Same goes for me. Changing the velocity removes some gravity apparently.

Re: Having a problem

Posted: Thu Sep 11, 2008 9:58 pm
by rude
I don't know what kind of horrible things setVelocity does, but I guess you could use applyForce to apply the same force as gravity?

Re: Having a problem

Posted: Mon Sep 15, 2008 6:37 am
by cag
Setting velocity changes the instantaneous velocity of the body in respect to the frame of reference of the world, so if you tell the object to have a velocity of (16, 0), it will at that very instance have that velocity. If you want the object to retain the downward velocity it has already gained from gravitational acceleration, you're gonna have to preserve the gravitational velocity while adjusting the horizontal velocity.

Also, from PIL: "Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all of its results. When we use a call as an expression, Lua keeps only the first result."

So when you do

Code: Select all

self:setVelocity( self:getVelocity() + envy.vector:new(16, 0) );
you're actually discarding the vertical component of the original vector.

Suggested:

Code: Select all

vx, vy = self:getVelocity()
self:setVelocity(vx + 16, vy)

Re: Having a problem

Posted: Mon Sep 15, 2008 9:51 pm
by Kuromeku
No, I'm not discarding it.

entity:setVelocity is a function I made that looks like this:

Code: Select all

function entity:setVelocity(velocity)
    self.b_Body:setVelocity(velocity.x, velocity.y);
end;
Where 'velocity' is a vector created with kudoLib's vector library. That is why when you see:

Code: Select all

entity:getVelocity() + envy.vector:new(16, 0)
It actually adds it to it, retaining the previous vertical velocity.

Thank you for your input however.

Re: Having a problem

Posted: Tue Sep 16, 2008 1:25 am
by cag
Huh. That's weird.
Well, you can always apply impulse (mass * change in velocity).

Re: Having a problem

Posted: Tue Sep 16, 2008 10:43 pm
by Kuromeku
It is not wierd, it is handy. You should check out my vector library - it is so much easier working with vectors because you can easily add them, multiply them, or whatever.

Re: Having a problem

Posted: Wed Sep 17, 2008 1:57 am
by cag
I meant that your code should work as intended, and that applying impulse instead of messing with velocity directly may solve the ball's problem.