Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Help with iPhone game gravity factor

zorzenzorzen Posts: 0New Users
Hello, I am making an iPhone arcade style game. It's not using the cocos2d but just Xcode. This is kind of a long thread but I've been trying to fix this for a while and could really use your help. I have an object, box, and a platform, platform, that the box sits on. When a button, up, is pressed the box jumps. The gravity named gravity is defined as kGravity with 0.01 force to push down on box when it jumps so that it falls back on the platform. It is not supposed to fall through the platform. I have gravity working, but jump is weird and this may be due to how I made sure the box didn't go through the platform, but it may be something else. Jump is weird because the jump button pressed the first time or after waiting before pressing it again launches the box up very slowly, but then pressing it after it goes way to low and drops back on to the platform very quickly. boxVelocity is what tells the box where to go and how fast, up or down. Here is my code:
-(void)characterGravity {
if(CGRectIntersectsRect(Box.frame, Platform.frame))
{
boxVelocity.y += 0;
}
else
{
boxVelocity.y += gravity.y;

Box.center = CGPointMake(Box.center.x + boxVelocity.x, Box.center.y + boxVelocity.y);
}
}

Here is the void to let the box jump:

-(void)up { Box.center = CGPointMake(Box.center.x, Box.center.y -10);}

and here is the viewDidLoad Code just in case:
-(void) viewDidLoad
{
gameState = kgameStateRunning;

boxVelocity = CGPointMake(0, 0);
gravity = CGPointMake(0, kGravity);

[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];

}


I don't think you'll need the .h to help me solve this because everything is doing what they should, I just haven't told/coded the right thing to make them do what I'd want. I'd also like help making sure that the top border of the screen is unpassible by Box. If you could help me with either of those I'd be extremely thankful.

Thank you for reading through, please respond if you can help :)
Post edited by zorzen on

Replies

  • architectpianistarchitectpianist Posts: 128Registered Users
    Here's how I would try it, remembering some stuff from Algebra I:
    Set up an NSTimeInterval to show how many seconds have elapsed since the jump started (set it to 0 when the jump finishes) and a BOOL for whether the jump is happening. In your game loop, see whether the box is jumping and if so, use a formula like "h = -28t^2+120t" (a quadratic formula for a falling or thrown object) to figure out the height off the ground. "t" is the time elapsed, "h" is the height, 28 stands for the force of gravity and 120 is the initial velocity. You may have to tweak those numbers a bit, and do some math to figure out the actual pixel location from the height, but hopefully the equation will give you a realistic jumping effect. And before you actually set the new height, you can test for collisions like this:
    if (!CGRectIntersectsRect(Box.frame, Platform.frame) && Box.frame.origin.y > 0.0)
    {
    //Change the height using the formula
    }
    Hope this helps!
Sign In or Register to comment.