开发者

Objective C Game Geometry question

I'm creating simple game and reached the point where I feel helpless. I was good in geometry but it was long time back in school, now trying to refresh my mind.

Let's say i have iPad screen开发者_StackOverflow. Object's xy position at one given point of time and xy position at another point of time stored in 2 variables .

Question: how to find the third position of the object at the end of the screen being given previous 2 position, considering the object moves in the same direction (line) from point 1 to point 2.

Thanks in advance.


Let us have that v1 and v2 are the vectors representing the two points. Let t0 be the time between the two points. Let t be the current time.

Then our location vector v3 is given by v3 = v1 + (v2 - v1)t/t0


If the object is moving in the same direction and you have an horizontal line, the next position given x and y would be

x+1, y

If the object is moving in the same direction in a vertical line it would be

x, y+1

If the object is moving in a diagonal up-right

x+1,y+1

diagonal down-right

x+1, y+1

diagonal down-left

x-1, y-1

diagonal up-left

x-1, y+1

So something general would be :

newPosition = (x+1,y) //if you wish to move forward to the right, try to handle all    
                        cases

All the cases above work if the object is moving forward, if it is moving backwards just change the + by - . Basically think of the object as moving in a cartesian coordinate system, where x is horizontal and y is vertical.

I think you can get the idea out of this three cases ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜