## Temple Run Magnet Effect

Apprentice
Posts: 6
Joined: 2012.07
Post: #1
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!
⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #2
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.)
Member
Posts: 338
Joined: 2004.07
Post: #3
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.)

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian