Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

image and collision out of place
Started by citizengoosevv Jul 31 2019 05:19 AM

1 reply to this topic
collision physics image sticker-knight
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

citizengoosevv

[GLOBAL: userInfoPane.html]
citizengoosevv
  • Observer

  • 5 posts
  • Corona SDK

Hello! Im new to corona and lua, so excuse me if this a really stupid mistake.

 

I downloaded the Sticker Knight template and tried to make the knight shoot by adding in the following to the hero.lua file:


	function instance:shoot(bx, by)
		local bullet = display.newImage("bullet.png", bx, by)
		bullet.type = "bullet"
		bullet.collision = bulletCollision
		bullet:addEventListener("collision", bullet)
		physics.addBody(bullet, "dynamic", { radius = 25, density = 1, friction =  5})
		bullet:applyLinearImpulse(50, 0)

		local function bulletCollision(self, event)
			if event.target.type == "bullet" and event.other.type == "enemy" then
				print("ow")
			end
		end
		
	end

that function is then called when space is pressed and sets bx and by to the player position.

But the result is really weird. 

When I shoot, I only get invisible collisions and the sprites are all out of place with the collisions. Its like it takes the display coordinates and the player position as different things.

 

Im really confused as to what I should do.



[TOPIC: post.html]
#2

XeduR @Spyric

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

  • 985 posts
  • Corona SDK

I don't know if you are using this yet, but debugging physics issues gets a lot easier by using this

physics.setDrawMode( "hybrid" )

There are certain pitfalls with physics and display groups. All of the physics simulations are run on the default coordinate system. This means that they will work as expected as long as you don't start moving individual groups that contain physics objects.

 

The physics bodies are owned by the physics engine and they don't support the display groups per se. If all of your physics bodies that need to collide with one another are in the same display group, you won't run into any issues as all of the contained objects will move the same distance. However, if you have physics objects in different display groups and you start moving those, then you may run into issues.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: collision, physics, image, sticker-knight