Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
3  
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? –  Tetrad Nov 20 '11 at 5:25
    
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; –  BarakatX2 Nov 20 '11 at 9:50
    
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? –  Trevor Powell Nov 20 '11 at 11:28
    
I've updated the question. Hopefully the information is more useful. –  XSL Nov 20 '11 at 14:31
    
@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. –  XSL Nov 20 '11 at 14:35
add comment

1 Answer

up vote 1 down vote accepted

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)))
share|improve this answer
    
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. –  XSL Nov 20 '11 at 18:23
    
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. –  Ilmari Karonen Nov 20 '11 at 20:01
add comment

Your Answer

 
discard

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.