Make transition react to collsion contact

This is way too complicated to describe.

I’ll make a small demo of what I think you want, then I’ll share it here.

You can tweak it after that, but my help beyond that will be limited.

I understand completley.

Thank you so much!  :slight_smile:

Warning I will use SSK because I have no time for doing things long-hand.

Heading out.  Example will be posted later tonight.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/shooterMechanics.zip

https://www.youtube.com/watch?v=llV2NdQh9Ls&feature=youtu.be

Be aware.  This is not drop-in friendly.  It is merely a demo of concepts, and then only a few.  There are many  more ways to move objects w/ physics.

Thanks a lot @roamingammer! Sorry for the late reply I have exams and stuff. I am going to check it out and try to implement it. Thanks again!

@roaminggamer thanks again for your help!

I am trying to make the enemy fire bullets automatically.

I added this code before “return public”

local function createEnemyBullet() local enemy\_bullet = display.newImageRect( "images/bullet.png", 20, 20 ) enemy\_bullet.x = enemy.x enemy\_bullet.y = enemy.y enemy\_bullet.rotation = enemy.rotation physics.addBody( enemy\_bullet, "dynamic", { radius = 10, filter = myCC:getCollisionFilter( "pbullet" ) } ) enemy\_bullet.isA = "bullet" -- Movement vector calculation. local vec = angle2Vector( enemy.rotation, true ) vec = scaleVec( vec, bulletSpeed ) -- Ssart moving enemy\_bullet:setLinearVelocity( vec.x, vec.y ) -- -- Hack to delete bullet in 3 seconds transition.to( enemy\_bullet, { alpha = 0, delay = 3000, time = 0, onComplete = display.remove } ) end Runtime:addEventListener("enterFrame",createEnemyBullet)

But it is not working.

Any idea on what I have to do?

If I recall, the enemy is not rotating to face the player.  So, you need to use the vector between the player and the enemy, not the enemies rotation, which is always 0 degrees. 

i.e.  something like:

 -- Movement vector calculation. local vec = diffVec( enemy, player ) vec = normVec( vec ) vec = scaleVec( vec, bulletSpeed ) 

You have also entirely forgotten about the need to update your collision rules.  If you have a new bullet type that hits the player, but not the enemy it should added to the collision calculator statement at the top of the file.

local myCC = ssk.cc:newCalculator() myCC:addNames( "player", "enemy", "pbullet", "ebullet" ) myCC:collidesWith( "enemy", { "player", "pbullet" } ) myCC:collidesWith( "player", { "enemy", "ebullet" } )

Use ‘ebullet’ when creating enemy bullet, not “pbullet”

physics.addBody( enemy\_bullet, "dynamic", { radius = 10, filter = myCC:getCollisionFilter( "ebullet" ) } )

Hi.  While I was willing to help this time, it looks like you’re just trying to just copy and paste the code I gave you.

You need to take a timeout and learn vector math if you want to have any chance of success which understanding physics based movement.

Here are some places to start:

There are many, many more resources on the web.

Also, dig into my 2d math library and be sure to understand what the ‘standard operations’ do.

Note: That PDF may be too hard a starting point unless you already have a solid math background. 

There is a wonderful site I wanted to suggest for you but I can’t find it in my resources list right now.

If I find it I’ll post back.

@roaminggamer ah thanks it worked.

I looked into the math2d guide previously but not the calculator guide.

Didnt know much about vectors (tbh only in g10 lol) so your link was very useful.

Anyways have also gone ahead to add collision listeners for the bullets and made them spawn automatically.

Only problem is that I am sometimes getting an error:

C:\Users\Busey\Documents\Corona Projects\shooterMechanics\scripts\ex1.lua:118: attempt to index upvalue 'enemy' (a nil value) 13:29:57.744 stack traceback: 13:29:57.744 C:\Users\Busey\Documents\Corona Projects\shooterMechanics\scripts\ex1.lua:118: in function '\_listener' 13:29:57.744 ?: in function '?' 13:29:57.744 ?: in function \<?:190\>

I am going to guess that this is caused when a bullet is spawned when the enemy does not exist.

Any idea what to do?

In two words: SCOPE and VISIBILITY

https://docs.coronalabs.com/tutorial/basics/scope/index.html

Last response of the day, time to work.

You need to not spawn if there is no enemy.  There are many ways to achieve this.  The simplest is to check if there is a valid enemy and if not, simply abort the spawn call early.  I am sure I had some code like that in the other parts of that sample.