event.x seems to not work - lua

I have this scene, I want when the earth collide with the blackhole an explosion sprite to be played.
So I wrote this:
local function onCollision( event )
if ( event.phase == "began" ) then
if(event.object1.name =="blackholeSprite" or event.object2.name =="blackholeSprite") then
explosionSprite.x=event.x
explosionSprite.y=event.y
explosionSprite:play()
timer.performWithDelay( 1500, gameOver )
end
end
end
The problem is that the explosion does not occur where the ball and the blackhole collide event.x and event.y . As you can see from the screenshot the explosion take place in the top-left corner. Any idea why this happens?

Maybe it is because of what is reported in the docs under "gotchas" for collision events.
Gotchas
The x and y position can be influenced by physics.getAverageCollisionPositions() and physics.setReportCollisionsInContentCoordinates().
Event position
During the "ended" phase (See the Collision Detection Guide), the X and Y positions are always zero. This is a Box2D limitation.
Also, when a collision involves a circle, and if the collision result is returned in local-space (see physics.setReportCollisionsInContentCoordinates(), then the local-space position of the collision is always 0,0. This is a Box2D limitation.
To solve your issue you could try to get the coordinates from (one of) the objects themselves, instead of getting them from the event.

Related

Roblox: Touched event returns incorrect position of projectile

I've made a cannon in Roblox, it uses part velocity to launch cannon ball projectiles out the cannon. It attaches a function to the touched event for the cannon ball that's taken from server storage and moved to workspace. The cannon ball when touched and the part touched is not part of the cannon itself generates an explosion.
The problem is that it works for roughly 20 shots, its a different amount of successful cannon shots each time however after a few the cannon ball's position starts return incorrectly and as a result the explosions explode in mid air.
To figure out the root cause of this I've tried removing the explosion and making it spawn a part there instead and after roughly 20 shots it will start to make the parts appear in mid air instead of where the cannon ball actually collided. I've tried both making the cannon ball collide and non collide which made no difference. It appears that the position within the touched event function is not returning the true position of where the part was touched in 3D space.
local function cannonMC1CTSEvent(player, mousePos)
local cannonBall = game.ServerStorage.cannonball:Clone()
cannonBall.Position = script.Parent.barrel.CFrame.Position
cannonBall.Parent = workspace
physicsService:SetPartCollisionGroup(cannonBall, "cannonBall")
script.Parent.barrel.CFrame = CFrame.new(script.Parent.barrel.CFrame.Position, mousePos)
cannonBall.CFrame = CFrame.new(script.Parent.barrel.CFrame.Position, mousePos)
cannonBall.Velocity = cannonBall.CFrame.LookVector * 256
local function generateExplosion()
explosion = Instance.new("Explosion")
explosion.BlastRadius = 2
explosion.BlastPressure = 100
end
generateExplosion()
cannonBall.Touched:Connect(function(touched)
if touched.Parent.Name ~= "cannon" then
explosion.Position = cannonBall.Position
cannonBall:Destroy()
explosion.Parent = workspace
generateExplosion()
end
end)
Here is the code for the cannon ball, the code fires a cannon ball every time the user clicks which is done locally using a remote event to tell server to fire the cannon. I've been baffled with this issue and my first guess would be that it's just Roblox Part.Velocity being buggy. Thankyou in advance!
I've spent about 5 hours today trying to find the root of the issue. It's the Roblox touched event. It is inaccurate at detecting collision of moving parts. I figured out this was the issue by checking whether ArePartsTouchingOthers was true or false, the times the explosion was in mid air and inaccurate to the visual position of the cannonball when it collides was false whereas when it was correctly positioned it was true. Therefore I am going to remake my code around using ArePartsTouchingOthers instead of using the touched event.
making an explosion before it touches something is a bad idea so try running the function when it collides. EDIT: you never gave the explosion a parent (but also don't pre-create the explosion) so try:
local function generateExplosion()
explosion = Instance.new("Explosion",game.Workspace)
explosion.BlastRadius = 2
explosion.BlastPressure = 100
end

Transition.to during "moved" event issue on device

So basically, I have a paddle that rotates around the screen based around where the user is touching. In the corona simulator, my code works flawlessly... on my test device... that's another story.
--All pseudocode for now but this is the gist of it
local function movePaddle(event)
local phase = phase
if phase == "began" then
--get angle of touch based on centerX/centerY
--get angle of paddle's current location
--get deltaAngle between the two using ((((touchAngle- paddleAngle)%360)+540)%360)-180
transition.to(paddle,{rotation = deltaAngle, time = 200, delta = true, transition = easing.outElastic})
elseif phase == "moved" then
--get angle of touch based on centerX/centerY
--get angle of paddle's current location
--get deltaAngle between the two using ((((touchAngle-paddleAngle)%360)+540)%360)-180
transition.to(paddle,{rotation = deltaAngle, time = 200, delta = true, transition = easing.outElastic})
end
end
On the simulator I have zero issues... When I run this on the device, the "began" phase works flawlessly but if a user drags his/her finger the paddle lags behind significantly. I've tried other easing functions to include not having one(which it defaults to linear iirc). My guess, is that when in the "moved" phase, the time in the transition.to is compounded.
Please help me out :( Is this a known issue with transition.to? I can provide exact code if needed.
Without seeing more of your code, it's possible that you're generating quite a few touch events which are creating quite a few transitions. I'm not sure I would transition the paddle, but just simply move it with the finger.

Corona SDK completely destroy physics joint

I am making a game in Corona sdk where different balls bounce and stick to each other with a weld joint. When I reset the gameplay, all joints are destroyed, then all the balls are repositioned. Here is some of my code:
--stickJoints and balls are tables that contain all the weld joints and ball objects
resetTotal = function()
for i=1,#stickJoints do
stickJoints[i]:removeSelf()
stickJoints[i]=nil
end
for i=1,20 do
--this resets all balls and sets linear and angular velocity to 0
resetP(balls[i],1000+(i-1)*400,5000,0)
end
end
The problem is that as soon as I tap the reset button, the balls that were connected temporarily interact with each other. Any thoughts are helpful. Thanks!
I think your best bet is either
to use timer.performWithDelay to call a function that will reposition the balls after the physics has had a chance to break the constraint.
Remove each ball from physics, and use a delayed function cal (see item 1) to re-add them after having been repositioned.

applyForce Speed going faster

Hey I have this problem where If I tap and hold the screen, the player gains speed going up on Y.
What I would prefer is that the player taps the screen (and holds to float up) and he goes up at steady speed (without getting quicker).
Here is the function for float speed and touch event:
function activateJets(ship,event)
ship:applyForce(0, -1.0, ship.x, ship.y)
print("run")
end
function touchScreen(event)
print("touch")
if event.phase == "began" then
ship.enterFrame = activateJets
Runtime:addEventListener("enterFrame", ship)
end
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", ship)
end
end
Runtime:addEventListener("touch", touchScreen)
Sorry if this doesn't make sense. Here is a general idea of what I want:
player touches screen (and holds)
object then floats up at consistent speed (no speed gain)
player releases touch
object drops normally
The physics involved prevents you from doing that: you are applying a constant force to your ship. According to Newton's laws (simulated by the physics library), this means a constant acceleration, thus a linear increase in speed.
The behavior you want (which is not coherent with real physics) is an instant acceleration to your target speed and then no speed variation. Thus it should be enough to set the speed of your ship to a constant value using shipSetLinearVelocity() inside your activateJets function. Of course you should reset the speed to zero when the touch has ended.

Drawing and appending lines in a single touch Corona SDK

Currently, I am trying to create a game with a similar logic with the famous One Touch Drawing. I already know how to draw lines after research.
Anyway, if for example, I have 4 points in my scene forming a square and I start drawing a line on one of these 4 points, how can I immediately append the line without lifting my finger to end the touch? Right now, this is the current logic of what I've done so far:
function drawLine(event,self)
if event.phase == "began" then
--do something here
elseif event.phase == "moved" then
local line = display.newLine(x1,y1,x2,y2)
if myflag == true then
line:append(.....)
end
end
end
I tried making a flag return true if the touch passes a point however this didn't work at all. How can I make it possible to append a line with one touch while drawing it? Thanks in advance.
i'm creating the same game using corona i'm using image for the pattern and then retracing it using newLine as for the points of each pattern i get the position of my points using the vertices of the pattern. see the image

Resources