Temple Run Magnet Effect

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?

Quote this message in a reply
⌘-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.)
Quote this message in a reply
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)

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
"It is better to be The Man than to work for The Man." - Alexander Seropian
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Using blending for a text fading effect. cjcaufield 2 7,332 Jun 30, 2010 08:21 PM
Last Post: cjcaufield
  Halo-Effect in 3D OpenGL ES Bersaelor 4 9,666 Dec 17, 2009 11:54 AM
Last Post: Mark Levin
  How to do a Quick, Dirty bloom effect FAST! Oddity007 12 15,821 May 9, 2009 09:42 AM
Last Post: aardvarc
  malloc particle effect (glDrawArray) Graphic Ace 1 4,659 Mar 15, 2009 03:13 PM
Last Post: maximile
  Turning Page Effect with OpenGl ReSuMa 3 8,346 May 12, 2007 05:57 AM
Last Post: ReSuMa