Transition.to during "moved" event issue on device - lua

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.

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

ROBLOX Camera Lock-on causes spinning

I made a Lock-On Script for ROBLOX that locks onto a specific enemy, but follows the player from a shoulder-surfing view point.
The problem is, when approaching the said enemy or entity that is locked on, and jumping on their head or above them; causes the player to spin when move only forward.
I'm aware that the problem comes from the movement system going towards where the Camera is looking, which is what I want; however, preferably with a seamless transition when moving above the enemy.
-- Declare variables
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")
local dummy = workspace.Dummy
local target = workspace.Lock
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = target
local angle = 0
-- Wait a frame for camera to load
wait()
-- Update camera on RenderStepped to get smooth motion
runService.RenderStepped:connect(function()
local character = player.Character
-- Check if character and torso exist (in case character is dead)
if character and character.Torso and character.Head then
local torso = character.Torso
local head = character.Head
local ZPosition = Vector3.new(head.CFrame.X, head.CFrame.Y + 2, head.CFrame.Z)
local ZCFrame = CFrame.new(head.CFrame.X, 1, head.CFrame.Z)
local XPosition = Vector3.new(head.CFrame.X + 5, head.CFrame.Y + 2, head.CFrame.Z)
camera.CFrame = CFrame.new(ZPosition, target.Position) * CFrame.new(2, 0, 7)
camera.Focus = CFrame.new(target.Position)
end
end)
MP4/GIF
The camera spinning when on top of the entity's head could be because you're trying to reach a point that you're vertically on top of.
To help you imagine this, visualize if you were trying to use a compass that points towards the north pole to reach the north pole, and you can only walk in the direction the compass is pointing. Once you got near the north pole, the compass would point towards it. But, if you kept walking forwards, eventually you'd walk OVER the north pole and pass over it. Then the compass would spin around and point backwards, so you'd walk backwards. Eventually you'd walk over top of it again and the compass would flip around again.
Eventually you'd get to a point where you're going back and forth over top of it very fast but you never get QUITE directly over top of it. Because the compass always points towards the north pole, you would see the compass spinning around very quickly because you're moving around it very quickly very close to it, but never exactly over top. The closer you get, the smaller movements make a bigger impact on the direction of the compass.
I think this is what's happening, where the camera is locked onto the target and the player is walking forwards. Once you get ON TOP of the target, the camera tries to keep pointing towards the target, but since you're so close, you pass back and forth over the point on the target the camera is pointing to and it whips around trying to point to it.
Maybe you could insert a check like this:
if (distanceToTarget.X >= 1 meter) then
camera.pointAtTarget(); --Point at the locked on target as your script would
else
camera.pointNormally(); --Point straight ahead as it normally would if there was no locked target
end
The distance is up to you to determine but the general idea is that if you get close enough to the target that the camera starts whipping all around, it will stop tracking and function normally. Alternatively, if the issue only occurs when ABOVE the target, then try including another condition to check if the player is directly above the target and to stop tracking then.

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.

event.x seems to not work

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.

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