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:
- Great video: CLICK ME
- This is the WHOLE COLLECTION
- Another great easy to follow video: CLICK ME
- Don’t have to read whole thing (at once): A PDF see chapter 2
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.
This is the problem of using transition.* or manual changes to <x,y> with physics.
You are essentially bypassing the physics engine. i.e. It is not made aware (in a reasonable amount of time) of the position changes, so it can’t react correctly.
The ONLY way to properly solve this is to move with velocities and forces.
A hack to try to overcome this is to:
- set ‘isSleepingAllowed’ on bodies to false.
- set ‘isBullet’ on moving bodies to true.
This is still a hack, but maybe it will be OK.
HOWEVER, you cannot use transition.* or manual updates to cause one body to push another body. That won’t work.
The hack didnt work. Trying out to make the enemy follow the previous bomb/bullet instead of obj.
Is there anyway to make a make moving object get pushed by another moving object?
Yes, as I said, use all physics movement.
Is there a guide for that? I looked it up but i am not sure if you mean this:
https://docs.coronalabs.com/api/type/Body/applyLinearImpulse.html
No. Just the physics guideand all the physics docs (1, 2) . You need to work out what it means to push a thing and then write the correct physics code to do it. Sorry, but I can’t be more exact that that.
This demo shows one example of pushing and how transition.* does it wrong whereas physics does it right.
Top is transition.*
Bottom is physics.*
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/transition_vs_physics.zip
( VIEW FULL SCREEN TO SEE ** DIFFERENCE**)
https://www.youtube.com/watch?v=tY91oSeOJro&feature=youtu.be
THanks I will look into this.
By the way, instead of using transitions, would changing the x and y coordinates work like:
local function move() enemy.x=enemy.x+1 enemy.y=enemy.y+1 end move()
Also would I need a collision listener? I dont have one in the example I gave above.
Dude. You didn’t read post above.
No. It won’t work. That is you manually changing <x,y>
You can only achieve correct pushing of physics bodies with forces and velocities.
Sorry for my impatience, but this is such a huge topic, without knowing exactly what you want to do, it is hard to instruct you.
So my only hope is to give you some examples that do something, then let you experiment.
Once you have a firm grasp of the physics.* system and some basic understanding of vector math, making action games in Corona is a snap.
Until then, it is completely impenetrable and any success is accidental. You must learn these things and experiment a lot with all the different field settings and functions. There is no quick path to knowing this stuff. 
PS - You may believe your original post is exact, but it raises all kinds of questions:
- Is there gravity
- If so, do the objects ignore it or respond to it? Do only some ignore it?
- How are you going to decide when a body should stop moving?
- Does it need to move along a specific path.
- Is it ballistic.
- Should some bodies bounce off others or only push?
- How fast is the object moving.
- …
I added another example where both objects are moving with transition.* or physics.* to show you how transitions fail when moving physics objects that interact/touch/collide.
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/transition_vs_physics2.zip
In a nutshell, transition.* wins over physics.
Right sorry about that.
After reading your code and watching the video, i sort of get it.
In my case, the pusher is the bullet and the enemy is the circle.
Only issue is in my project; the “circle” is moving to the pusher while the pusher to the circle.
So after thinking about it, I realised that the bullet does react to the collision by getting pushed back but the enemy does not.
So what I have to do is to make a collision listener where when the collision ended, the enemy will be pushed back by a transition and when that is done, it will resume transitioning to the enemy.
Only question is…how do i do it?
I don’t agree. You need to abandon using transition.* as the means of movement. Otherwise you’re going to spend all your time trying hack around things that don’t work.
If you’re making a shooter you want to user pure physics.
See my second example:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/transition_vs_physics2.zip
It shows a major issue with transition.* and physics.
Are you trying to make something like ‘Soul Knight’?
No no that was an example for the moving mechanism.
Should i use then setLinearVelocity( ) instead of transition.to?
And if I do use setLinearVelocity, how can I make it got to a specific point?