I want to make a moon rotating around a planet, or something along that analogy. So is there something I can call for physics.setGravity(0,0) that changes the position that the gravity pulls towards, particularly assigning that to be a physics body? If not, simply a specific x-y coordinate will be fine.
local moon = display.newImage ("moon.png")
physics.addBody(moon, {bounce=0, density=1.0})
local earth = display.newImage ("earth.png")
physics.addBody(earth, {bounce=0, density=1.0})
Thanks
Instead of playing around with the setGravity option, I think you should just rotate the images using something like this:
local physics = require("physics")
physics.start()
physics.setGravity(0, 0)
display.setStatusBar( display.HiddenStatusBar )
local background = display.newImage( "space.png", true )
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
local earth = display.newImage( "earth.png" )
earth.x = 100; earth.y = 200
earth.xScale = 0.7
earth.yScale = 0.7
physics.addBody( earth, {radius=40 } )
earth.bodyType = "static"
local moon = display.newImage( "moon.png", 40, 40)
moon.x = earth.x + 80
moon.y = earth.y
moon.xScale = 0.3
moon.yScale = 0.3
moon.rotation = 100
physics.addBody( moon, {radius=10 } )
moon.bodyType = "dynamic"
myJoint = physics.newJoint( "pivot", moon, earth, earth.x, earth.y )
local orbit = function( event )
moon.rotation = moon.rotation + 1.5
end
Notice that I am not really mucking about with setGravity, but the speed of the rotation is defined in the orbit function.
The above code assumes you just have image files to represent your planetary bodies. If so, you will have to play around with the constants above (earth.x and earth.y, scaling values, etc) to make the whole system LOOK right for the images you choose.
Good luck!
No - you can change gravity but it will pull/push only in the direction you have it set to, never to any point. You'd likely use a sensor (invisible) around your earth and apply force to draw things towards it when the collision event between the moon/other and sensor began.
Related
I'm having some issues with moving a character physics capsule in local space. No matter what rotation I give it, it still moves in relation to world space. I've attached a mesh component, a character physics component, an input component with my input, and a lua script to it. Here's the movement code in my lua script:
function PlayerController:HandleInput(floatValue)
local currentBusId = InputEventNotificationBus.GetCurrentBusId()
local forwardSpeed = 0.0
local sideSpeed = 0.0
local rotate = 0.0
local fixedSpeed = self.Properties.Speed * 0.01
local fixedStrafeSpeed = self.Properties.StrafeSpeed * 0.01
if(currentBusId == self.forwardBusId) then
forwardSpeed = floatValue
end
if(currentBusId == self.leftBusId) then
sideSpeed = -floatValue
end
if(currentBusId == self.rotateBusId) then
rotate = floatValue
end
PhysicsComponentRequestBus.Event.AddImpulse(self.entityId, Vector3(fixedStrafeSpeed * sideSpeed, fixedSpeed * forwardSpeed, 0.0))
end
I was wondering what the best way to move it in local space would be.
If you wish to move a character in local space then you will need to compute it based on the orientation of your character. In C++, it would be like:
AZ::Transform myLocation;
TransformBus::EventResult(myLocation, GetEntityId(), &TransformBus::Events::GetWorldTM);
const auto q = Quaternion::CreateFromTransform(myLocation);
const Vector3 disp = q * AZ::Vector3::CreateAxisY( 1.f );
myLocation.SetTranslation(myLocation.GetTranslation() + disp);
Ok, so I'm trying to create hexagons for my game. The first option I had is to have several images of hexagon, but I'm having problems with clickable area since these images are positioned side-by-side.
So i guess my only option is to create objects using polygons. Here is a code from corona sdk's website:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, }
local o = display.newPolygon( halfW, halfH, vertices )
o.fill = { type="image", filename="mountains.png" }
o.strokeWidth = 10
o:setStrokeColor( 1, 0, 0 )
That code is for creating a star. But I don't know how to create a hexagon using vertices.
Try this to create the vertices array:
local R = 45
local N = 6
local vertices = {}
local i = 0
for t = 0, 2*math.pi, 2*math.pi/N do
i=i+1; vertices[i]= R*math.cos(t)
i=i+1; vertices[i]= R*math.sin(t)
end
And this to draw the hexagon:
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local hexagon = display.newPolygon( halfW, halfH, vertices )
hexagon.fill = { type="image", filename="mountains.png" }
hexagon.strokeWidth = 10
hexagon:setStrokeColor( 1, 0, 0 )
I chose R=45 to produce a polygon of the same size of your star.
You could always use a graphics.newMask() to apply a mask to each image hex that would make the outside area not touchable.
I made a equilateral triangle:
local W = 540
local H = 700
local r = 150
local r_x = math.sqrt(math.pow(r,2)-math.pow((r/2),2))
local vertices = { 0,-r, r_x , r/2 , -r_x , r/2 }
local block = display.newPolygon( W, H, vertices )
physics.addBody( block )
physics.setGravity(0,0)
block.angularVelocity= 100
I want the triangle to rotate on its axis but it does not happen. why?
I thinks it related to the object's anchor but I dont know how to fix this.
Try yourself if you don't understand what I mean.
Just take all the objects and insert them into a group. Then just rotate the group using the rotate API.
I have an image (ground) with transparent pixels on top of it since the ground has hills like ups and downs ... the problem is that the ball isn't moving down and up according to the hills ... the ball is just floating over the image. what can I do ??
local bg2 = display.newImage("images/ground.png",true)
bg2:setReferencePoint(display.CenterLeftReferencePoint)
bg2.x = 0; bg2.y = _H/1.25;
local physics = require("physics")
physics.start()
local redOrb = display.newCircle( 0, 0, 25)
redOrb:setFillColor(255,0, 0)
physics.setGravity(0,9.8)
redOrb.x = 180; redOrb.y = 190; redOrb.rotation = 5
physics.addBody( bg2, "static", { friction=0.5, bounce=0.3 } )
physics.addBody( redOrb, { density=3.0, friction=0.5, bounce=0.3 } )
When you add a body to an image that has a transparency , it will not automatically make the body the as the image itself.
You can use this tool for the physics body
http://www.codeandweb.com/physicseditor
http://www.codeandweb.com/blog/2012/05/24/getting-started-with-coronasdk-and-physicseditor-tutorial
To view the actual physics body you can add this code
physics.setDrawMode("hybrid")
Can someone explain why the rectangle doesn't spin here? This is Lua code and I'm using Corona SDK.
That is, I've tried to set up a rectangle with it's centre of mass off-center, and then apply a force to it in the middle, expecting it to spin as it's center of mass is off....
display.setStatusBar( display.HiddenStatusBar )
-- Setup
local screenCenterX, screenCenterY = display.contentWidth/2, display.contentHeight/2
-- Create Object
local myRect = display.newRect(0, 0, 30, 90)
myRect.strokeWidth = 2
myRect:setFillColor(140, 140, 140, 0)
myRect:setStrokeColor(180, 180, 180)
myRect:setReferencePoint(display.CenterReferencePoint)
myRect.x, myRect.y = screenCenterX, screenCenterY
-- Apply Physics
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
physics.addBody( myRect, "kinimatic", { friction=0.5, bounce=0.1, radius = 45 } )
-- Redefine Centre of Mass (what I'm trying to get right)
myRect.xOrigin, myRect.yOrigin = screenCenterX, screenCenterY - 100
-- Replace myRect to the center as setting the xOrigin/yOrigin seems to have moved it
myRect.x, myRect.y = screenCenterX, screenCenterY
-- Apply Force
timer.performWithDelay(3000,
function(event)
myRect:applyForce(5,0, screenCenterX, screenCenterY)
-- WHY DOES THIS NOT SPIN THE OBJECT???
-- Centre of gravity has been change so shouldn't it rotate now?
-- That is, trying to simulate applying a force to an object who's centre of mass is NOT in
-- the center, and then see it spin.
end
)
The variables xOrigin and yOrigin are used only to move a object, those variables cannot affect physics at all, and they are not really a "origin" (they are the x/y position of a object in relation of the parent "x/yOrigin" variable).
To do a unbalanced physics object, you need to do it with the shapes, so you will have to create two shapes (thus you cannot use automatic shapes, you will have to use polygon), with one of them being heavier than the other to simulate the off-center mass.
For example a rectangle with 100 units in length, you make 50 units weight 1, and the other 50 units weight 2, the result would be the center being at x 75 more or less (my math may be wrong here).