Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Make transition react to collsion contact
Started by JoePringles Apr 09 2018 01:49 PM

- - - - -
26 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

I have a moving object:

 transition.to( enemy, { time=3000, x=obj.x-50,y=obj.y-50})

I have a bomb/bullet

local bomb=display.newImageRect("bomb_boost.png",20,20)
bomb.x=obj.x
bomb.y=obj.y

They both have physic bodies

physics.addBody(enemy,"kinematic",{radius=75,density=0,friction=0,bounce=0.5})
physics.addBody(bomb,"dynamic",{density=1,friction=0,bounce=0,isBullet=true})

To make this simple, the bomb is moved by a transition.

transition.to(bomb,{time=1000,x=enemy.x,y=enemy.y})

When the enemy is still(not moving), the bullet reacts properly(enemy bounces back).

However when the enemy is moving and the bullet is moving, they do not react.

 

Anyway to make them react when both ibjects are moving.

 

Thanks bros.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

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?



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

Yes, as I said, use all physics movement.



[TOPIC: post.html]
#5

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

No. Just the physics guide and 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)

 


Edited by roaminggamer, 09 April 2018 - 03:09 PM.


[TOPIC: post.html]
#7

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

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.



[TOPIC: post.html]
#8

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.

 

 

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.



[TOPIC: post.html]
#9

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.
  • ...

Edited by roaminggamer, 09 April 2018 - 03:28 PM.


[TOPIC: post.html]
#10

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.



[TOPIC: post.html]
#11

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

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?



[TOPIC: post.html]
#12

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.



[TOPIC: post.html]
#13

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

Are you trying to make something like 'Soul Knight'?



[TOPIC: post.html]
#14

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

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?



[TOPIC: post.html]
#15

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.


Edited by roaminggamer, 09 April 2018 - 04:16 PM.


[TOPIC: post.html]
#16

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

I understand completley.

 

Thank you so much!  :)



[TOPIC: post.html]
#17

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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



[TOPIC: post.html]
#18

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

Heading out.  Example will be posted later tonight.



[TOPIC: post.html]
#19

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

[TOPIC: post.html]
#20

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.



[TOPIC: post.html]
#21

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

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!

[TOPIC: post.html]
#22

JoePringles

[GLOBAL: userInfoPane.html]
JoePringles
  • Contributor

  • 370 posts
  • Corona SDK

@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?



[TOPIC: post.html]
#23

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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" ) } )


[TOPIC: post.html]
#24

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.


Edited by roaminggamer, 14 April 2018 - 08:59 AM.


[TOPIC: post.html]
#25

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,666 posts
  • Corona SDK

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.




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]