Hi,
There is a ball moving a long a path with obstacles that make the ball come up wards downwards and to the side. I want add a small amount of speed to the ball when it is in motion. I have been trying to multiply the speed and using linear impulses but they only have gone in one direction. I want to add speed so there will be speed in any direction the ball is moving towards. When you tap the button the ball will have a momentary jolt, like a linear impulse but im debating wether velocity is what i need. Any help ?
Thank you.
Some code i've been trying out.
1 2 3 4 5 6 7 8 9 10 11 12 | -- You can change the speed faster= 1 function ballSpeed(event) ball:setLinearVelocity( faster * (-- not sure what to add here, have been trying positions), faster * (-- not sure what to add here, have been trying positions) ) end Runtime:addEventListener("enterFrame", ballSpeed) |
Are you looking for something like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | --Here I get the stage with and height for scaling _W = display.contentWidth _H = display.contentHeight --Start physics local physics = require ("physics") physics.start() physics.setGravity(0 , 9.8) physics.setDrawMode("") --Display a floor local floor = display.newRect(0, 0, _W, 10) floor.x = _W * 0.5 floor.y = 300 physics.addBody(floor, "static", {bounce = 0.3, friction = 10}) --Display a ball local ball = display.newCircle(0, 0, 8) ball.x = _W * 0.5 - 150 ball.y = _H * 0.5 + 100 ball:setFillColor(255, 0, 0) physics.addBody(ball, "dynamic", {bounce = 0.2, friction = 10, density = 1, radius = 8}) --This function will move the ball continuously with increasing speed to the right local function move() ball:applyForce( 0.5, 0, ball.x, ball.y ) end Runtime:addEventListener("enterFrame", move) --After two Seconds the ball will jump local function jump() ball:applyForce( 0, 150, ball.x, ball.y ) end timer.performWithDelay(2000, jump) |
Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | -- You can change the speed local faster = 1 local speedX,speedY = ball:getLinearVelocity() function ballSpeed(event) ball:setLinearVelocity( faster * speedX, faster * speedY ) end Runtime:addEventListener("enterFrame", ballSpeed) |
Hello!
Yes! This is almost what i want. The only problem is i want a momentary jolt for 1 second. Is this possible ? Thanks a lot.
Just use body:applyLinearImpulse() instead of body:setLinearVelocity() like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | -- You can change the speed local faster = 1 local speedX,speedY = ball:getLinearVelocity() function ballSpeed(event) ball:applyLinearImpulse( faster * speedX, faster * speedY, ball.x, ball.y ) end Runtime:addEventListener("enterFrame", ballSpeed) |
MichaelAssadi,
Just change the interval of the timer.
1 2 | --You can change the time to what ever you want. Here it will wait 1000 milliseconds or 1 second. timer.performWithDelay(1000, jump) |
Hello calebr2048,
Unfortunately adding the linear impulse did not work for some reason. It is still acting the same :I. Is there a problem?
It actually might be the linear velocity ? Because it gives a jolt but then start bouncing very quickly on the surface of the ground, it's like the gravity is pulling it.
Sorry if im bothering you and appreciate you help. I basically want to press a button and it gives the jolt in the area it's going. So far it has both, the only problem is when you press the button and the ball has a jolt the ball basically sticks to an area. I want it to merely bounce off and continue, but the ball is clinging to whatever it touches. Do you think if we add, linear velocity = false once we press the button that will do the trick? Or add a timer?
Thanks a lot. I hope you understand what im saying.
Don't worry you're not bothering me at all, I'm just trying to help.
/EDIT/
Sorry, I re-read your post and realized the code I posted would not work. Here is the revised code:
1 2 3 4 5 6 7 | local faster = 1 -- Call ballSpeed function when button is pressed local ballSpeed = function(event) ball:applyLinearImpulse( faster * speedX, faster * speedY, ball.x, ball.y) end |
Thanks for the code, but it's still not working!
I have the code here. And commented it, so you know what im doing. All you need is one image, for the button. Thanks! I think you will notice the problems. All i would like is when you press the button, the ball will have the impulse of speed with the velocity which you have done :) I guess it's just something to fix now. The ball is bouncing a lot, and does not stop after a while, i just want there to be an impulse then returns to normal as in no more speed or velocity. Thanks again.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | local ui = require("ui") display.setStatusBar( display.HiddenStatusBar ) local physics = require ("physics") physics.start(true) --physics.setDrawMode "hybrid" physics.setGravity(15, 0) local ball = display.newCircle( 0, 0, 18) ball:setFillColor(0, 255, 0) ball.x = display.contentHeight/7 ball.y = display.contentWidth/7 -- This makes the ball drop (dynamic) local button1Press = function( event ) physics.addBody(ball, { density = 0.2, bounce=0.3, radius = 15, friction=0.5}) end local drop = ui.newButton{ default = "drop.png", over = "drop.png", onPress = button1Press, emboss = true } drop.x = display.contentWidth/14 drop.y = display.contentHeight/12 drop.rotation = -90 -- The button to add speed to the ball. --If you try it out you will see it bouncing, or attaching to an objecta bouncing very quickly. -- local button2Press = function( event ) local faster = 0.01 local speedX,speedY = ball:getLinearVelocity() -- I think it's the velocity? That's the reason i wanted a timer. function ballSpeed(event) ball:applyLinearImpulse( faster * speedX, faster * speedY, ball.x, ball.y ) end Runtime:addEventListener("enterFrame", ballSpeed) end local drop1 = ui.newButton{ default = "drop.png", over = "drop.png", onPress = button2Press, emboss = true } drop1.x = display.contentWidth/14 drop1.y = display.contentHeight/1.1 drop1.rotation = -90 -- Draw a line (swipe on the simulator) local i = 1 local tempLine local ractgangle_hit = {} local prevX , prevY local function runTouch(event) if(event.phase=="began") then if(tempLine==nil) then tempLine=display.newLine(event.x, event.y, event.x, event.y) -- Random Colors for line r = math.random (0, 255) g = math.random ( 0, 255) b = math.random (0, 255 ) tempLine:setColor(r,g, b) prevX = event.x prevY = event.y end elseif(event.phase=="moved") then tempLine:append(event.x,event.y-2) tempLine.width=tempLine.width+0.9 ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y) ractgangle_hit[i]:setColor(r,g, b) ractgangle_hit[i].width = 5 -- Creates physic joints for line (Turn on draw mode to see the effects) local Width = ractgangle_hit[i].width * 0.6 local Height = ractgangle_hit[i].height * 0.2 -- Physic body for the line shape local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height} physics.addBody(ractgangle_hit[i], "static", { bounce = -1, density=0.3, friction=0.7, shape = lineShape}) prevX = event.x prevY = event.y i = i + 1 elseif(event.phase=="ended") then tempLine.parent.remove(tempLine) tempLine=nil end end Runtime:addEventListener("touch", runTouch) |
Is this more along the lines of what you wanted?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | local ui = require("ui") display.setStatusBar( display.HiddenStatusBar ) local physics = require ("physics") physics.start(true) --physics.setDrawMode "hybrid" physics.setGravity(15, 0) local ball = display.newCircle( 0, 0, 18) ball:setFillColor(0, 255, 0) ball.x = display.contentHeight/7 ball.y = display.contentWidth/7 -- This makes the ball drop (dynamic) local button1Press = function( event ) physics.addBody(ball, { density = 0.2, bounce=0.3, radius = 15, friction=0.5}) end local drop = ui.newButton{ default = "drop.png", over = "drop.png", onPress = button1Press, emboss = true } drop.x = display.contentWidth/14 drop.y = display.contentHeight/12 drop.rotation = -90 -- The button to add speed to the ball. --If you try it out you will see it bouncing, or attaching to an objecta bouncing very quickly. --// NEW COMMENT \\-- -- Is this more along the lines of what you wanted? -- I removed the Runtime listener as I don't think it is necessary. local button2Press = function( event ) local faster = 0.1 local speedX,speedY = ball:getLinearVelocity() ball:applyLinearImpulse( faster * speedX, faster * speedY, ball.x, ball.y ) end local drop1 = ui.newButton{ default = "drop.png", over = "drop.png", onPress = button2Press, emboss = true } drop1.x = display.contentWidth/14 drop1.y = display.contentHeight/1.1 drop1.rotation = -90 -- Draw a line (swipe on the simulator) local i = 1 local tempLine local ractgangle_hit = {} local prevX , prevY local function runTouch(event) if(event.phase=="began") then if(tempLine==nil) then tempLine=display.newLine(event.x, event.y, event.x, event.y) -- Random Colors for line r = math.random (0, 255) g = math.random ( 0, 255) b = math.random (0, 255 ) tempLine:setColor(r,g, b) prevX = event.x prevY = event.y end elseif(event.phase=="moved") then tempLine:append(event.x,event.y-2) tempLine.width=tempLine.width+0.9 ractgangle_hit[i] = display.newLine(prevX, prevY, event.x, event.y) ractgangle_hit[i]:setColor(r,g, b) ractgangle_hit[i].width = 5 -- Creates physic joints for line (Turn on draw mode to see the effects) local Width = ractgangle_hit[i].width * 0.6 local Height = ractgangle_hit[i].height * 0.2 -- Physic body for the line shape local lineShape = {-Width,-Height,Width,-Height,Width,Height,-Width,Height} physics.addBody(ractgangle_hit[i], "static", { bounce = -1, density=0.3, friction=0.7, shape = lineShape}) prevX = event.x prevY = event.y i = i + 1 elseif(event.phase=="ended") then tempLine.parent.remove(tempLine) tempLine=nil end end Runtime:addEventListener("touch", runTouch) |
More along the lines??? That's it! Thank you so much! Right now it looks plain. The games is a lot more interesting i assure you ;). Are you in the process of making a game? Because if you are i would be very interested to see it. Im sorry if im being pushy but it has to do with the same concept, i have some sort of "portal" in my game. There are two on the screen a device where once the ball touches one of them it saves the velocity. I was thinking once collision with one of them it saves the velocity in a variable. Then pops out of the other portal on the screen with the same velocity? Do you know how this could be done? It's basically like a door where you come in one side then come out the other.
Thanks a lot for everything!
If your busy please don't feel pushed or anything on this. Thanks again.
Sounds about right. You could create two identical physics objects and place one at the same location as the exit portal but make it invisible. When the first object touches the portal you could copy the velocity to a variable and then remove the object. Then make the second object visible and add the velocity of the first object to it. I don't have time to write any code right now but I will give it a shot when I get home from work.
I am "trying" to work on a game although it's not even playable yet.
Thanks for all your help.
Sorry for the delay, I've been a bit busy. Here is some hacked together portal code. Yes I know it's ugly (I only worked on it for about 15 minutes) but it "works". Obviously it needs a lot of work to look convincing but it's a start.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | local physics = require("physics") physics.start() physics.setGravity(0,5) local portal1 = display.newRect(150, 100, 50, 50) portal1:setReferencePoint(display.CenterReferencePoint) local portal2 = display.newRect(150, 400, 50, 50) portal2:setReferencePoint(display.CenterReferencePoint) portal2.id = "portal2" local ball = display.newCircle(175, 200, 20) ball:setFillColor(255,0,0) local ball2 = display.newCircle(0, 0, 20) ball2:setFillColor(255,0,0) ball2.isVisible = false ball2.x = portal1.x ball2.y = portal1.y -- PHYSICS OBJECTS -- physics.addBody(portal1, "static", {isSensor=true}) physics.addBody(portal2, "static", {isSensor=true}) physics.addBody(ball, "dynamic", {radius=20}) physics.addBody(ball2, "static", {radius=20}) local onCollision = function(event, self) local speedX, speedY = ball:getLinearVelocity() if event.phase == "began" then if event.other.id == "portal2" then ball2.bodyType = "dynamic" ball2:setLinearVelocity(speedX, speedY) ball2.isVisible = true if ball.x == portal2.x then ball.isVisible = false end end elseif event.phase == "ended" then ball:removeSelf() end end ball:addEventListener("collision", onCollision) |
That's great! I just made one changed ball2:setLinearVelocity(speedX, speedY) into "ball2:getLinearVelocity(speedX, speedY)". I had a question, instead of making the body type "dynamic", do you know if there is any other way? Maybe adding an impulse could do it, with the same velocity. Now that i think of it, we could use the same code we had before. I think adding a linear impulse to it could do the trick. Thanks a lot! Hey, sorry for the questions... Im 13 years old and started about 3 months ago. Thanks for all your help! I hope someday i can help you ;). Hope your game works out!
It will almost the same as the impulse when you touch the button code right? For the portal ?
You got it! Yes an impulse would work too or if you want a more gradual speed increase you could use applyForce instead. The code would be similar to the impulse button code. Don't be sorry for asking questions, that's one of the best ways to learn. Is this your first experience with programming? You're lucky to have a tool as powerful and easy to use as Corona at your age. When I was 13 it was practically impossible to make games unless you at least had a degree in computer science.
If you have more questions you can e-mail me at calebdev2048@gmail.com since I don't won't to keep cluttering this forum thread.
Yes it is unfortunately. I wish i started even younger. Ya, i'm so glad there's a lot of programming tools out here like corona for any age. This is my new hobby now, and it's fun so it's great! I'm trying to release a game soon. Thanks a lot calebr2048, i hope i don't have any more questions :). Thanks for all your help! You've been great!
That is my other account i logged into on accident, i made that one before on accident. Just incase of confusion.
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Download Annoying Pig Game for Android - Please Download for FREE
Adobe Illustrator Class, Photoshop Class - Online
Runtime error 'nil' with unusable stack trace
Photo Camera trouble Corona 319 (media.show(media.Camera, onComplete))
Tech support, HELP, please!
How To Disable UI Button -- Please Help
gps- within-a-range detection
Thoughts on creating a Flash version of game for marketing
Spawn Balloons