iDevGames Forums
Temple Run Magnet Effect - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Temple Run Magnet Effect (/thread-10839.html)



Temple Run Magnet Effect - anmetv - Dec 1, 2013 08:35 AM

Hi All,

I've tried searching for this online but can't find a tutorial anywhere. I want the ability for the character to collect coins & other items within a specific radius. The only game I've seen this is in Temple Run (money magnet)

Anyone have a tutorial or code?


Thanks!


RE: Temple Run Magnet Effect - SethWillits - Dec 1, 2013 09:18 AM

If you already know how collision detection works, you're half way there. You can easily do spherical intersection testing by simply calculating the distance between each coin and the player. If it's within the radius, then handle the pickup. (Immediate, animate it, whatever.)


RE: Temple Run Magnet Effect - JustinFic - Dec 1, 2013 05:24 PM

A simplified version of the way I do it in Laserface is this (pseudocode)

Ppos = Player position (vector)
Cpos = Coin position (vector)
Cvel = Coin Velocity (vector)
Mspeed = Magnet Speed (scalar)

Code:
toPlayerVector = Cpos - Ppos;
if (toPlayerVector.Magnitude < MagnetDistance) {
    newVelocityVector = toPlayerVector.Normalize();
    Cvel = newVelocityVector * MSpeed;
}

When the player gets within MagnetDistance of the coin, the coin will move at a constant velocity directly at the player. Obvious assumptions: MagnetDistance is greater than the pickup collision radius, MSpeed is greater than the player's speed.

I do a couple extra things such as adding a second coefficient that decays exponentially with distance (giving more of a gravitational effect) and if you really want to get gravitational with it, you add the velocity to Cvel instead of set it (but this can have the effect of a coin whipping around the character and being flung back out of the magnet radius without ever being collected.)