physics addbody and display.newGroup

`local shturmGroup = display.newGroup()
local function createMinBoss()

  local POSPOSX = cx
  local POSPOSY = 400

  local LeftLaserGroup = display.newGroup()
  **LeftLaserGroup.x=POSPOSX-55**

** LeftLaserGroup.y=POSPOSY+5**
shturmGroup:insert(BleftLaser)

  local RightLaserGroup = display.newGroup()
  **RightLaserGroup.x=POSPOSX+55**

** RightLaserGroup.y=POSPOSY+15**
shturmGroup:insert(BrightLaser)

  local LLaser = display.newRect(**LeftLaserGroup **, 0, 0, 10, 500)
  LLaser.anchorY=0
  LLaser.rotation=-20

  local RLaser = display.newRect(**RightLaserGroup** , 0, 0, 10, 2000)
  RLaser.anchorY=0
  RLaser.rotation=20

  physics.addBody( LLaser, "static" )
  physics.addBody( RLaser, "static" )
end`

In resuat body located in 0,0 coordinates

You forgot to ask your question, but I can see what you’re doing wrong.

The groups all must be aligned. You cannot have physics bodies in different groups where the groups’ <x,y> positions differ.

This is in the documentation, where I don’t recall, but definitely there.

My Questions For You:

  1. Why are you changing the alignment of the groups?
  2. What are you hoping to achieve?
  3. What problem are you hoping to solve?

Knowing these things, I and/or others may be able to suggest a solution.

The groups all must be aligned. You cannot have physics bodies in different groups where the groups’ <x,y> positions differ.
This is in the documentation, where I don’t recall, but definitely there.

It can be found at: https://docs.coronalabs.com/guide/physics/physicsSetup/index.html

When working with Corona display groups and Box2D, it’s important to remember that Box2D expects all physics objects to share a global coordinate system . Both grouped and ungrouped display objects will work well since they will share the internal coordinates of that group. However, unexpected results will occur if physical objects are added to different display groups and those groups are moved, scaled, or rotated independently of each other. As a general rule, do not alter the position, scale, or rotation of display groups that contain physics objects.

2 Likes

I created an enemy in which some parts must rotate, + I create a laser. I grouped the boss objects with the laser object. And he placed the group at the turning point.

I’d add the physics body to the parent group. Also, try a radius body.

Like @roaminggamer already said, you still forgot to ask a question. :stuck_out_tongue:

If you need to group physics objects together, then consider using multi-element bodies.

My problem is that I’m trying to use physics and displaced groups together. Because of what the body appears not there. I use a group so that several objects rotate together. I moved the group to the pivot point. The crutch that I made - I add the coordinates of the object (to which I need to add the physical body) and the coordinates of the group, I create the same object but in the general group (not displaced). As a result - a copy of the object to which I need to add physical. body, but in another group, not visible and with a physical body(used google transliter :sweat_smile:)

local mainGroup = display.newGroup()

local GroupOffset = display.newGroup()
GroupOffset.x = 100
GroupOffset.y = 100

local Laser = display.newRect(GroupOffset, -50, 0, 10, 200)
LLaser.anchorY=0
LLaser.rotation=-20

Laser.body = display.newRect(mainGroup, GroupOffset.x+Laser.x, GroupOffset.y+Laser.y, Laser.width, Laser.height)
Laser.body.alpha = 0
Laser.body.myName = "laser"
Laser.body.anchorY = Laser.anchorY
Laser.body.rotation = Laser.rotation
physics.addBody( Laser.body, "static" )

The question is to place the physical body on an object in an offseted group. Or another way to rotate multiple objects so that they are “glued”.