Having problems understanding body positioning in Box2d. Any pointers on how things work? - cocos2d-iphone

I'm struggling to learn and understand box2d. Here is my problem:
I'm trying to make a simple car, the car body looks like this for now:
Now I'm trying to add a wheel, I create the wheel body and I assume I'll link the wheel to the car body using a b2RevolutionJoint.
Until I get to that part I just want to be able to position the wheel in its place, so in relation to the bodyCar Body. How do I do this correctly?
What I've seen online is something like this:
bodyDef2.position.Set(axle2->GetWorldCenter().x + 1.3*cos(-M_PI/3), axle2->GetWorldCenter().y + 0.3*sin(-M_PI/3));
However, I don't understand the cos and sin parts of that expression.
What I'm trying to do is this:
backWheelDef.position = b2Vec2(carBody->GetWorldCenter().x - 20.0/PTM_RATIO,carBody->GetWorldCenter().y);
Since my carBody vertices definition looks like this:
b2Vec2 vertices[] = {
b2Vec2(100.0/PTM_RATIO,15.0/PTM_RATIO),
b2Vec2(100.0/PTM_RATIO,30.0/PTM_RATIO),
b2Vec2(50.0/PTM_RATIO,65.0/PTM_RATIO),
b2Vec2(20.0/PTM_RATIO,65.0/PTM_RATIO),
b2Vec2(0.0/PTM_RATIO,45.0/PTM_RATIO),
b2Vec2(0.0/PTM_RATIO,15.0/PTM_RATIO)
};
The result of the above way of positioning the wheel looks somehow correct (see below screenshot) but if you look at the above numbers it doesn't make much sense.
Since the carBody width is 100.0/PTM_RATIO I assume the GetWorldCenter().x would return a value equal to 50.0/PTM_RATIO.
So the wheel position would be after calculation at 30.0/PTM_RATIO, right?
Well, if you look at the result below the wheel x positioning seems to be at 20.0/PTM_RATIO.
Can someone please explain how things work with positioning bodies in relation to other bodies works in Box2D? Once I'm clear with these kind of things I'll move on to linking them using joints
Thanks a bunch!
LATER EDIT BASED ON iforce2d ANSWER:
Thank you iforce2d for answering and thank you for all the good insight you provide in the box2d world. What you're saying is perfectly true but this wasn't my question.
Once I've figured out that GetWorldCenter returns the center of gravity and GetPosition returns the position origin of the object it was easy to place my wheel exactly where I needed it to be in relationship to the carBody. Even though I still got the behaviour of it appearing under the body I knew this would be fixed once I add joints into equation.
Now I have another question that somehow relates to this positioning problem. Take a look at the image below:
Is this the right way my joints should look once I set their anchor points as below? I'm a little confused by them going from origin instead of them going from anchorPointA to anchorPointB
Thanks again!

You are placing the wheel inside the car body to begin with, so in the first two or three time steps the two bodies will move to correct the overlap. It probably happens so fast that you don't see it, but the tell-tale signs are there, namely the wheel being positioned exactly at the edge of the car, and the car body being rotated a tiny bit.
If you pause the simulation (just don't call Step) you should see the bodies where you expect to see them.
Once you add the joint, the default behavior is for the two joined bodies not to collide with each other, so you wont see this problem.

Related

OpenGL - skeletal animation - bone rotates around origin

I am following a tutorial series about skeletal animation on Youtube (https://www.youtube.com/watch?v=f3Cr8Yx3GGA) and have ran into a problem – everything works fine, except when I rotate one of the bones (or "joints"), they get rotated around the scene origin, meaning they do not stay in place but are translated. The following image illustrates the problem:
How can I make it so that the translation doesn't happen? I have been going over the tutorial series multiple times now, but cannot identify which step would prevent this from happening.
The code is very large, split into around a dozen files, and I don't know which section might be causing the issue, so I do not think there's much point in posting it all here (it should be similar to the code in the tutorial, even though I am using C++ while he's working in Java. The tutorial code can be found here: https://github.com/TheThinMatrix/OpenGL-Animation). If you could give me even general advice on how this issue is normally solved in skeletal animation, it should hopefully be enough for me to at least identify the part that's wrong and try moving from there.
Rotation matrices on their own can only describe rotations around the origin (Wikipedia). However, rotations can be used in conjunction with translations to change where the origin is to get the desired effect. For example, you could:
Translate the object so that it is centered around the origin
Rotate the object to the desired orientation
Translate the object back to the original position
Or, to phrase it in a different, but functionally equivalent way:
Move the origin to the object's position
Rotate the object to the desired orientation
Reset the origin back to its original position
Related question: Rotating an object around a fixed point in opengl
You just need to pay attention to what you are rotating around.
A way to fix this: Rotate it first and then translate it. Rotate the object while it is at the origin and then translate the object to where you want it.
Repeatedly do this when things change throughout your program. Start the object at the origin, do the desired rotation, and then translate out to it's final resting position.

Changing the displayed direction of an image while it's moving in OpenGL

I'm doing a little something in openGL now and I'm trying to make it so that whichever direction an object is heading, that object displays that direction clearly.
e.g.
If an object that is a dog is heading SW, it should be pointing southwest.
Is there any simple way to do this? glRotatef seems like it would be a valid option at first but I feel like it would get sticky when trying to calculate the angle and all. Especially since this would need to be updated constantly.

Strange Issue with Open GL and rotations. Has anyone see this before?

So I have been here before asking about this issue but no one has been able to answer my question thus far. So I made a video that demonstrates the issue.
I have so far checked and rechecked my math files, so has two of my college professors, I have checked my shaders, and I am just using a simple pass through shader.
I have create a video of the issue happening, I will describe a little of my process.
I have set initial values of my rotations to, this pushes it to the point where the issue appears.
rotX = 0.0f;
rotY = -22.0f;
rotZ = -62.0f;
These values ensure that I do not have to rotate the object at run time, these values are where the problem occurs. As you can see in the video the cow will start to rotation without any place in the code actually telling it to. I output the rotations matrices and rotations values to my log file and they are all normal.
http://www.youtube.com/watch?v=LB7_zftalT8
So I have never seen this before ever, anywhere. If anyone has seen something like this let me know. I have not posted any code because there are far to many possible issues, however I can provide code snippets on demand, or provide access to my source svn.
I have spent three weeks debugging this issue, I have at least 10 other sets of eyes look at this issue and review my code.
The only issue floating around that I haven't been able to remove as an issue is memory corruption, I hope you have better luck.
Edit:
Code Samples:
http://pastebin.com/hRSvEYAZ
I use quaternions for my rotations, I create them with axis angles. I then multiple my rotations together and create my 4x4.
Edit:
I have tried the answer provided by MadCoreTom, it did not help, I have also continued to take a look at memory and I cannot see any issue.
Edit:
So I have removed the normalization calls from my quaternions, now I only normalize the axis when I create my quaternion from an axis angle. I have also rewritten my toMatrix3 for my quaternion to see if that helps, it hasn't.
I also moved my updating the rotations to inside my key press, I intend to provide more code soon as well.
It is possible that rounding errors during .normalize() are causing the values to change slightly, and this keeps building up until the cow goes crazy.
You could try something like the following, and use rx in place of rotationX within the update method:
rx = rotationX.normalized();
Instead of modifying the value each time you loop through.
Alright so I solved my issue. There was a floating point error covering up the actual issue.
So once I fixed that, not using the method provided by MadcoreTom though. What I ended up doing was only normalizing the the axis, and never normalizing my quaternion during rotations.
The real issue was I was storing my angle and accumulating it, this was causing the issue.
Now I just pass in the change in rotation, create my quaternion from the axis/angle change, then I multiply this to my quaternionF.

Minecraft like collision

I'm working on a Minecraft like game for educational purposes. The rendering is great so far even with 1024x1204 blocks but now that I started integrating the player collision I'm having problems.
I have a aabb for the player and aabb's for all the blocks around him. These are created dynamically and it works out pretty fast.
My problem goes as following:
I have speed vector and the current position. For each axis I calculate the potential position and make out an aabb. I check for collisions and it's free I move there otherwise I set the speed for that component to 0. I separate the axis since I want my player to slide in a direction of partially facing a wall.
The order for the axis is y,x,z. The collision response is great but I'm having some problems with the corners as it sometimes get's stuck in the world without being able to move. Not sure what the reason is for this.
I do not want to implement actual physics since those are more demanding and basically just too much for what I need.
Do you guys have any suggestions on how to implement this in a nice way? I did some searching but I didn't find anything useful for this particular situation.
This is a bit abstract in a sense that the cause of your problem can be related to many things. From the top of my head, maybe a bug in your collision detection code: somehow it allows the objects to cross boundaries by 1 (or more) unit. So when the next collision is computed 1 or more dimension is stuck (imagine having an arm already inside the wall when collision is detected. You can't get your arm out because it collide with the interior of the wall boundary)

How do I give a Box2d Kinematic body a predefined curved path

I'm new to using Box2d and really can't find a consistent answer to my question so please excuse me.
I'm writing a game for the iPhone -- who isn't these days -- and I'm using Cocos2d and Box2D. I have objects that move around in the game and I would like to give them a more of a curved path. From my studies I know that I should use body of type b2_kinematicBody which allows me to change the linear velocity every tick and keeps them from colliding with my other flying bodies. I do understand that if I know my current position (x,y) and my next position in the step (x1,y1) I would be able to compute the vector for velocity. Does anybody have a good way to give an object a defined curved path? I'm thinking it has to be some kind of parametric equation.
Take a look at bezier curves. After curve defenition you can compute the derivative (it's easy for bezier) and use it as a velocity for your body
OH whats the game called? sounds like a good one. have you looked into maybe just doing that in version 2.0