2
\$\begingroup\$

I'm trying to move two objects away from each other at a proportional distance, but on different scales. I'm not quite sure how to do this. Object A can go from position 0.1 to 1. Object B has no limits. If object B is decreasing, then Object A should be decreasing at rate R. Likewise, if Object B is increasing, then Object A increases at rate R. How can I tie these two Object positions together so that in an update loop, they automatically update their positions?

I tried using:

ObjA.Pos.X +=  0.001f * ObjB.VelocityY; //0.001f is the rate

This works but there's an error each time it runs. ObjA starts off at its max position 1 but then the next time it will stop at 0.97, 0.94, 0.91 etc.. This is due to the 0.001f rate I put in. Is there a way to control the rate, yet not end up with the rounding error?

EDIT - To clarify the scenario: The user can provide a force to Object B in a vertical motion (2D game). Gravity will bring the object back down once the user lets go of the up/down button. As the object moves up or down, I want object A to move left and right on the X axis. Maximum distance to the right can be '1', and to the left can be '0.1'. Therefore, if the user keeps holding up forever,object A will still only be at 0.1.

Object A at max 1 = Object B at position 4.5 (this is the starting position)

Object A at min 0.1 = object B at position +/- 9.9

Obejct B does take the game time into account.

\$\endgroup\$
5
  • 3
    \$\begingroup\$ You have to define limits for object B so that you can define your rate of movement. At what position is object B at so that object A is at its min and max position? \$\endgroup\$
    – Tetrad
    Nov 20, 2011 at 5:25
  • \$\begingroup\$ Is the game time being taken into consideration in ObjB.VelocityY? If the time since the last frame is in the calculation, then the rate can be set to units/sec rather than 0.001f per frame. EG: ObjA.Pos += 0.1f * ObjB.VelocityY * deltaTime; \$\endgroup\$
    – BarakatX2
    Nov 20, 2011 at 9:50
  • \$\begingroup\$ What do you mean by ObjA "stops" at 0.97, 0.94, etc.? If ObjB has no limits (as you state), then you should be able to just keep moving it in a positive direction until ObjA reaches its limit of 1.0, right? \$\endgroup\$ Nov 20, 2011 at 11:28
  • \$\begingroup\$ I've updated the question. Hopefully the information is more useful. \$\endgroup\$
    – XSL
    Nov 20, 2011 at 14:31
  • \$\begingroup\$ @TrevorPowell - I've added some extra information, but I found that when the user lets go and the object falls back down due to gravity, even though its position is 4.5, Object A's position keeps decreasing each time the user lets go of B. I got rid of the 0.001f and that fixed the problem, but Object A moves too fast without that damping. \$\endgroup\$
    – XSL
    Nov 20, 2011 at 14:35

1 Answer 1

1
\$\begingroup\$

You could just set object A's x coordinate directly based on object B's y coordinate:

ObjA.Pos.X = 1 - 0.9 * Math.Max(0, Math.Min(1, (ObjB.Pos.Y - 4.5) / (9.9 - 4.5)))
\$\endgroup\$
2
  • \$\begingroup\$ How do I speed up/slow down the rate of position change? At the moment, it's pretty much the same as my original attempt. If I change the value of 0.9, then the range doesn't remain between 0.1 and 1 for object A. \$\endgroup\$
    – XSL
    Nov 20, 2011 at 18:23
  • \$\begingroup\$ If you want to have A at 1.0 when B is at 4.5, and A at 0.1 when B is at 9.9, then that pretty much fixes the speed of A as (1.0 - 0.1) / (9.9 - 4.5) = 1/6 = 0.1666... times the speed of B. I suppose you could apply a non-linear map, so that A moves faster over some parts of its range than over others, but that's about it. \$\endgroup\$ Nov 20, 2011 at 20:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .