Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my XNA game I have two objects that collide. When the first object collides with the other it is able to latch on to it and move it about the world. I am having a problem with the math here (Math isn't my strong point). I currently have the second object latch on to the first and move around with it, but I cannot get it to keep it's original direction. So, if the object is facing up it should keep this direction relative to how it is being rotated with the original item.

enter image description here

Any tips on how I could best to achieve this?

share|improve this question
I think both objects would rotate about the axis of the center of the other object. – Byte56 Sep 26 '12 at 3:46

As far as angle is concerned, just keep the angle of the second object the same as the angle of the first. For position, when the two objects collide with each other, find the distance of the second object from the first using the distance formula.

distance = math.sqrt((x2 - x1)^2 + (y2 - y1)^2)

To find the position of the second object relative to the first object:

x2 = x1 + distance * math.cos(angle)
y2 = y1 + distance * math.sin(angle)
share|improve this answer

In addition to @tesselode's answer, I would recommend you keep a list of attached objects for every world object in your game. Something along the lines of List<WorldObject> AttachedObjects. Then, when you'd detect a collision, you would add the black rectangle to the red one's AttachedObjects. After that, it's only a matter of updating the children's positions using @tesselode's method every time you update the parent's position.

share|improve this answer

I'm assuming one of the two will have control over the other one, much like a fork-lift. In your drawing, I'm assuming this is the black one, and the red one is the package. You only mention you're having trouble with angles, so that's all I'll cover.

When the objects latch together, you should remember the angle difference between the two (I'll call this Theta). I have drawn this in figure 2 with the arrows from the first figure drawn over each other. Until the objects are unlatched, the angle of the Red object will be determined by the Black's angle plus Theta, as shown in figure 3.

// When Latching:
theta = Red.angle - Black.angle;

// When retrieving Red's angle:
Red.angle = Black.angle + theta

Angle Diff

share|improve this answer

You could implement an ability to have child objects for an object (where child objects would be automatically affected by changes to the position/rotation of parent). Then upon collision when they should latch together, you make an invisible object and make both of the attached objects its children. Then, instead of applying transformations (movement, rotation) on the individual objects, you apply it on the big one, OR you could still apply forces to the individual children objects, which would check "do I have a parent?", and if yes, the force would be sent to the parent (which would merge forces from all child objects into one vector it would apply to itself).

Also, this approach should properly handle their rotation, if you calculate center of mass for the invisible parent according to weights of the latched objects, which John's method doesn't seem to accomplish (joined object rotate around their new shared center of mass, not around center of mass of one of the objects), and you could easily nest this kind of behavior without any additional code required. It seems to be the more robust and flexible approach.

+condition if the force applied to child is strong enough to break the joint, and you can handle the "unlatching" of it by simply removing the parent->child relationship. If there's only one child left, you make it seperate too and remove also the invisible parent.

I can provide more details (with pictures and at least pseudocode and such) if you're interested.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.