Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Cant move collision position
Started by marek.radimersky Jul 01 2018 05:12 AM

3 replies to this topic
tiled android

Best Answer ldurniat , 01 July 2018 - 12:18 PM

Hi Marek,

 

This should solve problem

map:insert( ballGroup)

From Moving a display group wont move the physics?

 

@horacebury: What you should do is place all objects to be used as physics objects into the same display group OR make sure that you keep the parent display groups of physics objects at the same location relative to each other.

 

From Corona documentation

 

Display objects with physical bodies can, of course, be placed in different display groups for z-index layering purposes. However, if you need to detect collisions between objects in different groups, you should not move, scale, or rotate entire display groups independently of each other because of underlying Box2D functionality.
 
It is valid to move, scale, or rotate a series of groups in unison or lock-step as long as these groups maintain the same coordinate origin, angle, and scale. However, if groups are moved, scaled, or rotated independently of each other, the Box2D collision system will consider the physics bodies to be in their original state, not as they appear as a result of transforming the group.
 
This also applies to the physics.setDrawMode() "hybrid" and "debug" modes — even though these draw modes will suggest that the bodies have changed as a result of modifying the group, the collision system will still consider them to be in their original state.

 

 

 

ldurniat

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

marek.radimersky

[GLOBAL: userInfoPane.html]
marek.radimersky
  • Observer

  • 3 posts
  • Corona SDK

Hello, I am using Tiled to create maps for Corona and when I center the map to center of screen with this piece of code

map.x,map.y = display.contentCenterX - map.designedWidth/2, display.contentCenterY - map.designedHeight/2

then objects collision area stays on its original position as shown on images.


U7OkrCXETWm9YYaJjMw3OQ.png

 

lmc2EerYQo6nqD8nZ2k_wQ.png

 

Here is my code.

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

local tiled = require "com.ponywolf.ponytiled"
local physics = require "physics"
physics.setDrawMode("hybrid")
physics.start()
physics.setGravity( 0, 0 )

local json = require "json"
-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )
local strokes = 5
local strokesText
local ballSize = 12


-- Set up display groups
local ballGroup = display.newGroup()
local backGroup = display.newGroup()
local mainGroup = display.newGroup()
local uiGroup = display.newGroup()


--Load background
local background = display.newImageRect( backGroup, "texture/background/background.jpg", 1080 , 1920 )
background.x = display.contentCenterX
background.y = display.contentCenterY



local mapData = json.decodeFile(system.pathForFile("map.json", system.ResourceDirectory))  -- load from json export
--Load map
local map = tiled.new(mapData)
map.x,map.y = display.contentCenterX - map.designedWidth/2, display.contentCenterY - map.designedHeight/2

--Get array of wall objects
local walls_vert = map:listTypes( "wall_vert" )


--Add ball
local ball = display.newCircle(ballGroup, display.contentCenterX, display.contentCenterY, ballSize )
ballGroup:toFront()

--Add ball to physics
physics.addBody( ball, "dynamic", { radius=ballSize, isSensor=false, bounce=0.6, density = 10 } )
ball.myName = "ball"
ball.linearDamping = 1
--Add walls to physics

-- Display strokes left
  strokesText = display.newText( uiGroup, "Strokes: " .. strokes, display.contentCenterX, 80, native.systemFont, 36 )

--Function that moves ball depending on touch start and release
local begX
local begY
local endX
local endY
local finX
local finY
local function stroke(event)
  if ( event.phase == "began" ) then
    print( "Touch event began on: " .. event.x .. " " .. event.y )
    begX = event.x
    begY = event.y

  elseif event.phase == "moved" then
    --print( "Touch event moved on: " .. event.x .. " " .. event.y )
	elseif event.phase == "ended" then
    endX = event.x
    endY = event.y
    finX = begX - endX
    finY = begY - endY
    ball:applyLinearImpulse( finX * 0.5, finY * 0.5, ball.x, ball.y )
		print( "Touch event ended on: " .. event.x .. " " .. event.y )
	end
  --transition.to( ball, { linearDamping = 0.7, time = 250 } )
end
backGroup:addEventListener( "touch", stroke )

Any suggestions how to fix this issue?

Thank you in advance



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 325 posts
  • Corona SDK

  Best Answer

Hi Marek,

 

This should solve problem

map:insert( ballGroup)

From Moving a display group wont move the physics?

 

@horacebury: What you should do is place all objects to be used as physics objects into the same display group OR make sure that you keep the parent display groups of physics objects at the same location relative to each other.

 

From Corona documentation

 

Display objects with physical bodies can, of course, be placed in different display groups for z-index layering purposes. However, if you need to detect collisions between objects in different groups, you should not move, scale, or rotate entire display groups independently of each other because of underlying Box2D functionality.
 
It is valid to move, scale, or rotate a series of groups in unison or lock-step as long as these groups maintain the same coordinate origin, angle, and scale. However, if groups are moved, scaled, or rotated independently of each other, the Box2D collision system will consider the physics bodies to be in their original state, not as they appear as a result of transforming the group.
 
This also applies to the physics.setDrawMode() "hybrid" and "debug" modes — even though these draw modes will suggest that the bodies have changed as a result of modifying the group, the collision system will still consider them to be in their original state.

 

 

 

ldurniat


Edited by ldurniat, 01 July 2018 - 12:39 PM.

  • marek.radimersky likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,748 posts
  • Corona SDK

@marek,

 

Hi.  While you can directly assign the <x,y> position of objects with bodies and you can use transtion.* functions on objects with bodies, the physics system is not meant to be used that way.

 

1. If you move an object that has a static body after you create it and add the body(), the body probably won't move.

 

2. Likewise, if a 'kinematic' or 'dynamic' body has slept, changing the <x,y> values probably won't move the body.

 

You can solve this issue by setting the body's isSleepingAllowed flag to false.

 

Better however is to move bodies with forces and velocities: https://docs.coronalabs.com/api/library/physics/index.html#functions

 

3. You can get a clearer picture of where bodies are by rendering them:

https://docs.coronalabs.com/api/library/physics/setDrawMode.html

physics.setDrawMode( "hybrid" )

  • marek.radimersky likes this

[TOPIC: post.html]
#4

marek.radimersky

[GLOBAL: userInfoPane.html]
marek.radimersky
  • Observer

  • 3 posts
  • Corona SDK

Hi Idurniat and roaminggamer,

 

Thank you very much for so fulfilling answers. 

map:insert( ballGroup)

solved the problem.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: tiled, android