Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Attack Physic implementation
Started by viktorw Aug 22 2018 04:45 AM

4 replies to this topic
physics collision
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

viktorw

[GLOBAL: userInfoPane.html]
viktorw
  • Observer

  • 3 posts
  • Corona SDK

HI I am new to corona sdk and game dev. at the moment I am doing some learning on sticker knight example.

 

Wanted to ask how to implement attack physic and collision

 

Sticker-Knight-Platformer/blob/master/scene/game/lib/hero.lua

 

I have added an attack animation in the sprite and I am having trouble changing the physics body during the attack animation, the *hand* punch outside of the initially defined physic body size and it doesn't do anything at the moment , does anyone know a good way to implement collision event while the hand is punching

 

I have tried moving instance anchorX during the attack animation so that it will bump into the enemy* but it get jumpy and I don't think it's a good idea.

 

working example code that I can reference would be nice

 

Please advice, Thank you

 

    -- Add physics
    physics.addBody( instance, "dynamic", { radius = 54, density = 3, bounce = 0, friction = 1.0 } )
    -- Load spritesheet
    local sheetData = { width = 230, height = 200, numFrames = 20, sheetContentWidth = 920, sheetContentHeight = 1000 }
    local sheet = graphics.newImageSheet( "scene/game/img/sample.png", sheetData )
    local sequenceData = {
        { name = "idle", frames = { 1, 2, 3 }, time = 333, loopCount = 0 },
        { name = "attack", frames = { 20 } }
    }
    instance = display.newSprite( parent, sheet, sequenceData )
    instance.x,instance.y = x, y
    instance:setSequence( "idle" )
    instance:play()

 

 

 



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 934 posts
  • Corona SDK

You can create the hand separately and track it's collisions.

Another, perhaps simpler way would be to create a sensor in the location where the punch would occur, in relation to the character, and update it's location as the character moves. You can accomplish this via different means, such as multi-element body, joint or simply updating the sensor's coordinates, etc. Then, when the punching animation is at the point where you want to know if it hit something, just check for collisions.



[TOPIC: post.html]
#3

viktorw

[GLOBAL: userInfoPane.html]
viktorw
  • Observer

  • 3 posts
  • Corona SDK

good suggestion, I was able to get a sensor to detect event.selfElement . thanks



[TOPIC: post.html]
#4

viktorw

[GLOBAL: userInfoPane.html]
viktorw
  • Observer

  • 3 posts
  • Corona SDK

You can create the hand separately and track it's collisions.

Another, perhaps simpler way would be to create a sensor in the location where the punch would occur, in relation to the character, and update it's location as the character moves. You can accomplish this via different means, such as multi-element body, joint or simply updating the sensor's coordinates, etc. Then, when the punching animation is at the point where you want to know if it hit something, just check for collisions.

 

I encounter a problem while implementing this, it indeed able to trigger during collision but it did not trigger while it is overlapping with enemy do you have a solution ?

 

https://imgur.com/a/VsMMJE1



[TOPIC: post.html]
#5

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 934 posts
  • Corona SDK

Collision events have two phases: "began" and "ended".

In order to implement whether an attack like that, you could create a variable such as "local wouldHit = false". When the collision event begins, you'd set it to true. When the collision event ends, you'd set it back to false. Then, when the user presses the hit/attack button, you'd only need to check if "wouldHit" is true or false.




[topic_controls]
[/topic_controls]