Hey all, I am starting my new foray in game development with Corona, and I was hoping someone can give me some advice. I have a hero (Sprite) in the center of the screen, and I’d like to give him the ability to hit a nearby enemy (within certain amount of pixels). Can someone offer some advice on doing this? I was thinking of creating newRect’s on both right and left side of the hero and creating a function that runs when the user taps the screen to hit, and it would test if the enemy is within these Rects. Is this a good method? Or is there a different/easier approach you would recommend? I was thinking it may be possible to use the physics library, however, I am unsure how to proceed with this.
[import]uid: 132483 topic_id: 34262 reply_id: 334262[/import]
There are several utility libraries available that have “distance between” formulas – check the Share Your Code area here. One library that used to be available (it may still be, I don’t know) is called Rum. It has that formula available.
Here are the two lines that do the magic:
[lua]local factor = { x = pos2.x - pos1.x, y = pos2.y - pos1.y }
local dBetween = sqrt( ( factor.x * factor.x ) + ( factor.y * factor.y ) )[/lua]
So creating a function that returns the distance between two objects means you can loop through all objects and see which ones are close enough to attack. Either set that on a timer so it happens automatically or just check when your guy needs to attack.
Jay
[import]uid: 9440 topic_id: 34262 reply_id: 136242[/import]
Hi @JonPM,
If you choose to use the physics engine (and I’m not suggesting that you MUST use it), this could be accomplished fairly easy with a sensor object around the hero. You can explore the physics setup here:
http://developer.coronalabs.com/content/game-edition-box2d-physics-engine
Best regards,
Brent [import]uid: 200026 topic_id: 34262 reply_id: 136243[/import]
If you want to just you a couple of different detection methods:
http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/
[import]uid: 199310 topic_id: 34262 reply_id: 136283[/import]
There are several utility libraries available that have “distance between” formulas – check the Share Your Code area here. One library that used to be available (it may still be, I don’t know) is called Rum. It has that formula available.
Here are the two lines that do the magic:
[lua]local factor = { x = pos2.x - pos1.x, y = pos2.y - pos1.y }
local dBetween = sqrt( ( factor.x * factor.x ) + ( factor.y * factor.y ) )[/lua]
So creating a function that returns the distance between two objects means you can loop through all objects and see which ones are close enough to attack. Either set that on a timer so it happens automatically or just check when your guy needs to attack.
Jay
[import]uid: 9440 topic_id: 34262 reply_id: 136242[/import]
Hi @JonPM,
If you choose to use the physics engine (and I’m not suggesting that you MUST use it), this could be accomplished fairly easy with a sensor object around the hero. You can explore the physics setup here:
http://developer.coronalabs.com/content/game-edition-box2d-physics-engine
Best regards,
Brent [import]uid: 200026 topic_id: 34262 reply_id: 136243[/import]
If you want to just you a couple of different detection methods:
http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/
[import]uid: 199310 topic_id: 34262 reply_id: 136283[/import]