Как я могу исправить траектории объектов в моей игре? - PullRequest
0 голосов
/ 19 июля 2011

Я делаю игру для iPhone с джойстиком.У него есть корабль, который должен стрелять пулями по траектории джойстика, но по какой-то причине я не могу заставить пули двигаться в правильном направлении.Может кто-нибудь помочь мне понять, что я могу делать неправильно?

Вот мой код:

-(void) shootBulletFromShip:(Ship*)ship
{
    double degrees = [[[NSUserDefaults standardUserDefaults] objectForKey:@"lol"] doubleValue];    
    NSLog(@"%f",degrees);

    float fDegrees = degrees;

    velocity = CGPointMake(1, fDegrees);


    outsideScreen = [[CCDirector sharedDirector] winSize].width;

    self.position = CGPointMake(ship.position.x,ship.position.y);
    self.visible = YES;

    [self scheduleUpdate];
}

Ответы [ 2 ]

1 голос
/ 19 июля 2011

Я думаю, что вы неправильно задаете скорость своей пули в следующей строке.

velocity = CGPointMake(1, spread);

Если spread указывает угол, вы, вероятно, должны передать синус и косинус этого угла как x- и y-компоненты вашего CGPoint следующим образом.

velocity = CGPointMake(cos(spread), sin(spread));

Возможно, вам придется немного изменить это в зависимости от того, выражен ли ваш угол в радианах или градусах.

0 голосов
/ 21 июля 2011
// Calculate the angle of the touch from the center of the joypad
float dx = (float)joypadBG.position.x - (float)convertedPoint.x;
float dy = (float)joypadBG.position.y - (float)convertedPoint.y;

float distance = sqrtf((joypadBG.position.x - convertedPoint.x) * (joypadBG.position.x - convertedPoint.x) + (joypadBG.position.y - convertedPoint.y) * (joypadBG.position.y - convertedPoint.y));

// Calculate the angle of the players touch from the center of the joypad
float touchAngle = atan2(dy, dx);

// If the players finger is outside of the joypad, make sure the joypad cap is drawn at the // joypad edge.
if (distance > joypadMaxRadius) 
{
    [joystick setPosition:ccp(joypadBG.position.x - cosf(touchAngle) * joypadMaxRadius, joypadBG.position.y - sinf(touchAngle) * joypadMaxRadius)];
} 
else 
{
    [joystick setPosition:convertedPoint];
}

diff = ccpSub([joystick position], joypadBG.position);
float angleRadians = atanf((float)diff.y / (float)diff.x);

float angleOffset = CC_DEGREES_TO_RADIANS(90);

if(diff.x < 0)
{
    angleRadians += angleOffset;
}
else
{
    angleRadians -= angleOffset;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...