grenade throw?

Hi fellow developers,

I want to show the player visually where his grenade will drop if he throws it. and the longer he keep pressed the throw button the farther the grenade will drop.

Here is the sketch of what i want.

throw.jpg

Now how do i create this visual points showing the travel and drop of grenade?

PS: I will be using physics to throw the bomb.

Here are some posts in the Code Exchange that describe what you are attempting to accomplish:

http://developer.coronalabs.com/code/trajectory-plotting

http://developer.coronalabs.com/code/calculating-trajectory

Thanks Panc software, I found a great code from here http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/

But the problem is i am using the whole background as a field for plaer to walk on so i want the grenade to stop where the line stops without falling further. have a look at the following picture please…

explosion.jpg

How do i stop/explode the grenade at the end of the line please?

Here is the code…

-- Physics Demo: Predicting Trajectory | Version: 1.0 -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2013 Corona Labs Inc. All Rights Reserved. -- SPECIAL THANKS to Matt Webster (a.k.a. "HoraceBury") for the trajectory calculations! local physics = require("physics") ; physics.start() ; physics.setGravity( 0,9.8 ) --; physics.setDrawMode( "normal" ) display.setStatusBar( display.HiddenStatusBar ) --set up some references and other variables local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight --set up terrain and background local back = display.newImageRect( "sky.jpg", 1024, 768 ) ; back.x = cw/2 ; back.y = ch/2 ; back:scale( 1.4,1.4 ) local wallL = display.newRect( -ox, -oy, 40, ch+oy+oy ) physics.addBody(wallL, "static", { bounce=0.4, friction=1.0 } ) local wallR = display.newRect( cw-40+ox, -oy, 40, ch+oy+oy ) physics.addBody(wallR, "static", { bounce=0.4, friction=1.0 } ) local wallB = display.newRect( -ox, ch-40+oy, cw+ox+ox, 40 ) physics.addBody(wallB, "static", { bounce=0.2, friction=1.0 } ) local wallT = display.newRect( -ox, -oy, cw+ox+ox, 40 ) physics.addBody(wallT, "static", { bounce=0.2, friction=1.0} ) local msg = display.newText( "Touch, drag, and release to launch projectiles", 0, 0, "ContenuBook-Display", 40 ) msg:setTextColor(255,255,255,220) ; msg.x, msg.y = cw/2,110 local prediction = display.newGroup() ; prediction.alpha = 0.2 local line local function getTrajectoryPoint( startingPosition, startingVelocity, n ) --velocity and gravity are given per second but we want time step values here local t = 1/display.fps --seconds per time step at 60fps local stepVelocity = { x=t\*startingVelocity.x, y=t\*startingVelocity.y } --b2Vec2 stepVelocity = t \* startingVelocity local stepGravity = { x=t\*0, y=t\*9.8 } --b2Vec2 stepGravity = t \* t \* m\_world return { x = startingPosition.x + n \* stepVelocity.x + 0.25 \* (n\*n+n) \* stepGravity.x, y = startingPosition.y + n \* stepVelocity.y + 0.25 \* (n\*n+n) \* stepGravity.y } --startingPosition + n \* stepVelocity + 0.25 \* (n\*n+n) \* stepGravity end local function updatePrediction( event ) display.remove( prediction ) --remove dot group prediction = display.newGroup() ; prediction.alpha = 0.2 --now recreate it local startingVelocity = { x=event.x-event.xStart, y=event.y-event.yStart } for i = 1,140 do --for (int i = 0; i \< 180; i++) local s = { x=event.xStart, y=event.yStart } local trajectoryPosition = getTrajectoryPoint( s, startingVelocity, i ) -- b2Vec2 trajectoryPosition = getTrajectoryPoint( startingPosition, startingVelocity, i ) local circ = display.newCircle( prediction, trajectoryPosition.x, trajectoryPosition.y, 5 ) end end local function fireProj( event ) if ( event.xStart \< -ox+44 or event.xStart \> display.contentWidth+ox-44 or event.yStart \< -oy+44 or event.yStart \> display.contentHeight+oy-44 ) then display.remove( prediction ) return end local proj = display.newImageRect( "bomb.png", 64, 64 ) physics.addBody( proj, { bounce=0.2, density=1.0, radius=14 } ) proj.x, proj.y = event.xStart, event.yStart local vx, vy = event.x-event.xStart, event.y-event.yStart proj:setLinearVelocity( vx,vy ) end local function screenTouch( event ) local eventX, eventY = event.x, event.y if ( event.phase == "began" ) then line = display.newLine( eventX, eventY, eventX, eventY ) line.width = 4 ; line.alpha = 0.6 elseif ( event.phase == "moved" ) then display.remove( line ) line = display.newLine( event.xStart, event.yStart, eventX, eventY ) line.width = 4 ; line.alpha = 0.6 updatePrediction( event ) else display.remove( line ) updatePrediction( event ) fireProj( event ) end return true end Runtime:addEventListener( "touch", screenTouch )

I’m not going to troubleshoot the trajectory code as I assume since you got it from a Corona blog post that it is pretty tight.

As far as exploding the grenade at a specific point, you have a few options. The quick and dirty way would be to use a timer function to call an explode function at a specific time, and you would need to figure out what that time was to coincide with the grenade reaching a point on it’s trajectory.

Another more intuitive (and time-consuming) way would be to create a formula that calculates the amount of time it would take to get to the explosion point, given speed and trajectory.

Yet another way would be, since you are using physics, to place a sensor object in the trajectory path where you’d like the grenade to explode, and have it listen for collisions, and then have the grenade explode.

These are just three ways I thought up in five minutes. I’m sure there are several much better ways to get a point on a trajectory line. I guarantee the Googles has some formulas that Pythagoras may have come up with.

Happy coding!

I figured out the above problem but now i came across this new problem :S
 
I am using this virtual pad script by Alejandro Jim�nez Vilarroya of dunkelgames http://www.dunkelgames.com/templates/corona-sdk-analog-virtual-pad-template
 
It returns the value in degrees now how do i use it with the trajectory code above? so instead of touching and moving finger across the screen i use this virtual pad.

I would be very thanksful if you help me with this!

I don’t really understand how you would modify the trajectory code to operate with the joystick. At this point, I think you need to experiment with the joystick code and how you can get it to work with your desired result. 

I came up with an idea to create a dummy object and move it with joystick and use it’s position to update trajectory :d

Here are some posts in the Code Exchange that describe what you are attempting to accomplish:

http://developer.coronalabs.com/code/trajectory-plotting

http://developer.coronalabs.com/code/calculating-trajectory

Thanks Panc software, I found a great code from here http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/

But the problem is i am using the whole background as a field for plaer to walk on so i want the grenade to stop where the line stops without falling further. have a look at the following picture please…

explosion.jpg

How do i stop/explode the grenade at the end of the line please?

Here is the code…

-- Physics Demo: Predicting Trajectory | Version: 1.0 -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2013 Corona Labs Inc. All Rights Reserved. -- SPECIAL THANKS to Matt Webster (a.k.a. "HoraceBury") for the trajectory calculations! local physics = require("physics") ; physics.start() ; physics.setGravity( 0,9.8 ) --; physics.setDrawMode( "normal" ) display.setStatusBar( display.HiddenStatusBar ) --set up some references and other variables local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight --set up terrain and background local back = display.newImageRect( "sky.jpg", 1024, 768 ) ; back.x = cw/2 ; back.y = ch/2 ; back:scale( 1.4,1.4 ) local wallL = display.newRect( -ox, -oy, 40, ch+oy+oy ) physics.addBody(wallL, "static", { bounce=0.4, friction=1.0 } ) local wallR = display.newRect( cw-40+ox, -oy, 40, ch+oy+oy ) physics.addBody(wallR, "static", { bounce=0.4, friction=1.0 } ) local wallB = display.newRect( -ox, ch-40+oy, cw+ox+ox, 40 ) physics.addBody(wallB, "static", { bounce=0.2, friction=1.0 } ) local wallT = display.newRect( -ox, -oy, cw+ox+ox, 40 ) physics.addBody(wallT, "static", { bounce=0.2, friction=1.0} ) local msg = display.newText( "Touch, drag, and release to launch projectiles", 0, 0, "ContenuBook-Display", 40 ) msg:setTextColor(255,255,255,220) ; msg.x, msg.y = cw/2,110 local prediction = display.newGroup() ; prediction.alpha = 0.2 local line local function getTrajectoryPoint( startingPosition, startingVelocity, n ) --velocity and gravity are given per second but we want time step values here local t = 1/display.fps --seconds per time step at 60fps local stepVelocity = { x=t\*startingVelocity.x, y=t\*startingVelocity.y } --b2Vec2 stepVelocity = t \* startingVelocity local stepGravity = { x=t\*0, y=t\*9.8 } --b2Vec2 stepGravity = t \* t \* m\_world return { x = startingPosition.x + n \* stepVelocity.x + 0.25 \* (n\*n+n) \* stepGravity.x, y = startingPosition.y + n \* stepVelocity.y + 0.25 \* (n\*n+n) \* stepGravity.y } --startingPosition + n \* stepVelocity + 0.25 \* (n\*n+n) \* stepGravity end local function updatePrediction( event ) display.remove( prediction ) --remove dot group prediction = display.newGroup() ; prediction.alpha = 0.2 --now recreate it local startingVelocity = { x=event.x-event.xStart, y=event.y-event.yStart } for i = 1,140 do --for (int i = 0; i \< 180; i++) local s = { x=event.xStart, y=event.yStart } local trajectoryPosition = getTrajectoryPoint( s, startingVelocity, i ) -- b2Vec2 trajectoryPosition = getTrajectoryPoint( startingPosition, startingVelocity, i ) local circ = display.newCircle( prediction, trajectoryPosition.x, trajectoryPosition.y, 5 ) end end local function fireProj( event ) if ( event.xStart \< -ox+44 or event.xStart \> display.contentWidth+ox-44 or event.yStart \< -oy+44 or event.yStart \> display.contentHeight+oy-44 ) then display.remove( prediction ) return end local proj = display.newImageRect( "bomb.png", 64, 64 ) physics.addBody( proj, { bounce=0.2, density=1.0, radius=14 } ) proj.x, proj.y = event.xStart, event.yStart local vx, vy = event.x-event.xStart, event.y-event.yStart proj:setLinearVelocity( vx,vy ) end local function screenTouch( event ) local eventX, eventY = event.x, event.y if ( event.phase == "began" ) then line = display.newLine( eventX, eventY, eventX, eventY ) line.width = 4 ; line.alpha = 0.6 elseif ( event.phase == "moved" ) then display.remove( line ) line = display.newLine( event.xStart, event.yStart, eventX, eventY ) line.width = 4 ; line.alpha = 0.6 updatePrediction( event ) else display.remove( line ) updatePrediction( event ) fireProj( event ) end return true end Runtime:addEventListener( "touch", screenTouch )

I’m not going to troubleshoot the trajectory code as I assume since you got it from a Corona blog post that it is pretty tight.

As far as exploding the grenade at a specific point, you have a few options. The quick and dirty way would be to use a timer function to call an explode function at a specific time, and you would need to figure out what that time was to coincide with the grenade reaching a point on it’s trajectory.

Another more intuitive (and time-consuming) way would be to create a formula that calculates the amount of time it would take to get to the explosion point, given speed and trajectory.

Yet another way would be, since you are using physics, to place a sensor object in the trajectory path where you’d like the grenade to explode, and have it listen for collisions, and then have the grenade explode.

These are just three ways I thought up in five minutes. I’m sure there are several much better ways to get a point on a trajectory line. I guarantee the Googles has some formulas that Pythagoras may have come up with.

Happy coding!

I figured out the above problem but now i came across this new problem :S
 
I am using this virtual pad script by Alejandro Jim�nez Vilarroya of dunkelgames http://www.dunkelgames.com/templates/corona-sdk-analog-virtual-pad-template
 
It returns the value in degrees now how do i use it with the trajectory code above? so instead of touching and moving finger across the screen i use this virtual pad.

I would be very thanksful if you help me with this!

I don’t really understand how you would modify the trajectory code to operate with the joystick. At this point, I think you need to experiment with the joystick code and how you can get it to work with your desired result. 

I came up with an idea to create a dummy object and move it with joystick and use it’s position to update trajectory :d