Page 1 of 2

Moving a rogue body in one axis

Posted: Tue Nov 06, 2012 7:17 am
by Its2l82die
Hi all,
I have a problem with a rogue body with infinite mass and I want to move it with an accelerometer in the x axis and I want realistic collisions with objects that fall on it, what I tried is to change its position and apply velocity to it by myself every step (which is wrong,I know) but it messes the whole simulation if someone has a better idea I would be more than happy to hear it, the body must be infinite mass as it must stay on the Y axis and not react to forces, but other bodies hitting it should react to its velocity when its moving.

Here is an illustration of what I mean:
Image

Thanks for any answers in the matter,
Anton.

Re: Moving a rogue body in one axis

Posted: Tue Nov 06, 2012 9:53 am
by slembcke
No, that's correct actually. With rogue bodies you are supposed to update their position and velocity manually yourself if you want them to move. The velocity is just the change in position divided by time:

Code: Select all

cpBodySetVel(body, cpvmult(cpvsub(newPos, cpBodyGetPos(body)), 1.0/dt));
cpBodySetPos(body, newPos);

Re: Moving a rogue body in one axis

Posted: Tue Nov 06, 2012 11:35 pm
by Its2l82die
Hey thank you,
That fixed it, my problem was I forgot to take the time into consideration >.< how careless of me.
my other problem is the smaller bodies that fall on my rigid body they sometimes run through it cause of the
high velocity I guess...

How can I fix that? the rigid body is sort of like a box... and it has bounds like one, just lines... so sometimes the smaller bodies break these lines and run through them, will making the lines (bounds) into shapes with more area will help the problem?

Thanks again,
Anton

Re: Moving a rogue body in one axis

Posted: Wed Nov 07, 2012 10:25 am
by slembcke
To deal with missed, high-speed collisions, you have three basic options that you can mix and match:
  • Don't let things get moving so fast. You can set a velocity limit on a body.
  • Use a smaller (fixed!) timestep. A smaller timestep means objects don't move as far in a single step reducing their ability to move straight through another object. This is a useful article: http://gafferongames.com/game-physics/f ... -timestep/
  • Make your objects bigger/thicker.

Re: Moving a rogue body in one axis

Posted: Tue Nov 13, 2012 6:48 am
by Its2l82die
Thanks for the reply,
I though about the same solution but know, I want to do something else as I am using an accelerometer to control the rogue body I want to keep the feature of acceleration but I want to limit the acceleration and since its a rogue body I must do all the updates myself I cant just set body velocity limit I must add it myself,I am having trouble with it so I am asking for help I tried using physics of course, but I got confused.

Thanks for the help,
Anton

Re: Moving a rogue body in one axis

Posted: Tue Nov 13, 2012 9:16 am
by slembcke
You can set the limit pretty high. You just can't allow an object to move more than half it's minimum size in a dimension in a single timestep. If you use a smaller timestep, then the limit can be larger.

Re: Moving a rogue body in one axis

Posted: Tue Nov 13, 2012 11:20 am
by Its2l82die
OK, I have another question, first I'll give the example in your OneWay example you can see a ball going through a line but it doesn't go the other way around, now my question is: is it possible to do the same but for a square type shape, but to ignore just 2 parallel lines of the square, what I mean is illustrated here:

Image

The red are the parts that I want to ignore collision but I want the green areas to have collisions, I know I can fully ignore a collision with this code, taken from the OneWay example:

Code: Select all

if(cpvdot(cpArbiterGetNormal(arb, 0), cpv(0,-1)) < 0)
	{
		cpArbiterIgnore(arb);
		return 0;
	}
But I am interested to save the green area collision.

Thanks in advance,again,
Anton.

Re: Moving a rogue body in one axis

Posted: Tue Nov 13, 2012 12:07 pm
by slembcke
Sure, pretty much the same code to do that. The dot product sort of tells you if the direction of the vectors are pointing in the same direction. If both are length 0 and pointing in the same direction, it gives you 1. If they are pointing in the opposite direction -1. If they are at right angles, then 0.

So you could do something like this:

Code: Select all

if(cpfabs(cpvdot(cpArbiterGetNormal(arb, 0), n)) < 0.7)
That would ignore collisions with normals that are ~45 degrees away from the normal. If you specifically want up or down, you can just do cpfabs(cpArbiterGetNormal(arb, 0).y) < 0.7. The math works out the same if you look at what cpvdot() does.

Re: Moving a rogue body in one axis

Posted: Tue Nov 13, 2012 1:44 pm
by Its2l82die
I'm sorry but I don't quite understand this, cpArbiterGetNormal would normalize any vector I would give it, what I must put as n then? from what I see in here anything would be bigger than 0.7 thus making the shapes always collide in my case I want the green parts of the shape to be collidable from any angle but the red ones pass through, totally ignored.

Sorry for my ignorance,
I'll try to think too tomorrow,
Anton.

Re: Moving a rogue body in one axis

Posted: Tue Nov 13, 2012 1:57 pm
by slembcke
Whoops, sorry I meant cpfabs(cpArbiterGetNormal(arb, 0).y) < 0.7

"n" is just the axis that you want things to pass through. If you wanted it to be up and down, use cpv(0, 1) or cpv(0, -1). Either is fine because it's just using the absolute value. Up and down is just equivalent to the code above though. You really only need the dot product if it's on an arbitrary axis.