Ben Wilson
Ben Wilson

Reputation: 3137

Change the angle at which an object travels xcode iphone

I have an object that travels on the screen and I was just wondering if there's any way that I could change the angle on which it travels.

I have this atm

enemy.center = CGPointMake(enemy.center.x+pos.x,enemy.center.y+pos.y);

    if (enemy.center.x > 328 || enemy.center.x < 0)
        pos.x = -pos.x;
    if (enemy.center.y > 480 || enemy.center.y < 0)
        pos.y = -pos.y;

Any ideas welcome and also is it possible to change the angle of which it bounces of the sides at?

Upvotes: 0

Views: 329

Answers (1)

bnvdarklord
bnvdarklord

Reputation: 309

The line equation is

y = tan(angle)*x + b.

Given an angle and the current position of the object you can find the next position:

Let's say your object is on (6, 5), and you want it moving with a 45 degree angle. tan(45) = 1. So you have 5 = 6 + b -> b = -1. So the line the object should be moving on is y = x - 1.

So the next point could be (7, 6) or (5, 4) depending on the direction and amount of movement.

Upvotes: 1

Related Questions