Accelerate in certain direction?

Hello!

I wonder if i have a sprite that i want to accelerate in a direction, Like if i have an airplane i want to accelerate in the direction the airplane is "pointing", so i tell the game to get some velocity in negative y direction this would make the plane go "up", but if i then turn the plane and want the plane to change direction so it always accelerate in the direction where the plane points? How do i do this?

I hope you understand=)

Kind regards

Hey,

I had problems with doing this recently and wound up putting together the following code, which does exactly what you are asking.

You'll need a 50x50 png of an arrow pointing to the right in the folder.

The bit you're interested in in on lines 145 and 146.

I hope it helps.

Spider

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
physics = require("physics")
physics.start()
physics.setGravity(0,0)
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.
 
_H = display.contentHeight;
_W = display.contentWidth;
 
 
function player()
 
player1 = display.newImage ("arrow.png")
player1.x = _W / 2
player1.y = _H / 2
--player1.rotation = 90 -- comment out for thrust code, *** rotate player1 90 for skate code ***
 
physics.addBody(player1, "dynamic", {density = 0, friction = 0, bounce = 0})
player1.myName = "player1"
 
        function setplayerRot()
                        playerRot = 0
                local playerRotText = display.newText( "playerRot: " .. playerRot, 10, 0, nil, 15)
                playerRotText.x = _W / 4
                playerRotText.y = _H / _H + 50
                playerRotText:setTextColor(255,255,255)
                        function getplayerRot()
                                        playerRot = player1.rotation
                                        playerRotText.text = "playerRot: " .. playerRot 
                        end
                Timer1 = timer.performWithDelay(10,getplayerRot, 0)
        end
 
end
 
function radians()
        p1Radians = player1.rotation * math.pi / 180
        --print (p1Radians)
end
 
function velocity()
        velx, vely = player1:getLinearVelocity()
        
        vx = math.cos (p1Radians) * velx
        print (vx)
 
        
end
 
 
function buttons()
 
function lArrow()
 
leftArrow = display.newRect(0,0,50,50)
leftArrow.x = _W / _W + 50
leftArrow.y = _H - 50
leftArrow:setFillColor(0,255,0)
leftArrow.alpha = .75
 
ltouched = false
 
local function ltouch(event)
        if event.phase == "began" then
        ltouched = true
        elseif event.phase == "ended" then
        ltouched = false
end
end
 
local function lrot(event)
        if ltouched == true then
                player1.rotation = player1.rotation - 10
                elseif ltouched == false then return false
        end
 
end
 
leftArrow:addEventListener ("touch", ltouch)
Runtime:addEventListener("enterFrame", lrot) 
 
end
 
        function rArrow()
 
local rightArrow = display.newRect(0,0,50,50)
rightArrow.x = _W - 50
rightArrow.y = _H - 50
rightArrow:setFillColor(0,255,0)
rightArrow.alpha = .75
 
rtouched = false
 
local function rtouch(event)
        if event.phase == "began" then
        rtouched = true
        elseif event.phase == "ended" then
        rtouched = false
end
end
 
local function rrot(event)
        if rtouched == true then
                                player1.rotation = player1.rotation + 10
                        elseif rtouched == false then return false
                end     
        end
                
 
 
rightArrow:addEventListener ("touch", rtouch)
Runtime:addEventListener("enterFrame", rrot) 
 
end
        
 
 
                function thrustButton()
                
        local thrustb = display.newRect(0,0,50,50)
        thrustb.x = _W/2 - thrustb.contentWidth / 2 - 5
        thrustb.y = _H - 50
        thrustb:setFillColor(0, 0, 255)
        thrustb.aplha = .75
        
        ttouched = false
        cando = true
        
local function ttouch(event)
         if event.phase == "began" then
        ttouched = true
        elseif event.phase == "ended" then
        ttouched = false
                cando = true
end
end
 
local function ttap(event)
        if ttouched == true then
                if cando == true then
                
                        vel = 50 -- this is the arbitrary amount of velocity to increase player motion by.
                        vely = -500
                        p1Rads = player1.rotation * math.pi / 180 -- step 1
 
                        local velX = math.cos (p1Rads) * vel -- upper case (velX/velx) to feed into obj:setLinearVelocity(X,Y)
                        local velY = math.sin (p1Rads) * vel
                        print(velX,velY,player1.rotation)
                                
                                
                                
                                player1:setLinearVelocity(-velX,-velY)
                                
                                        local function stopMovement()
                                                player1:setLinearVelocity(0,0)
                                        end
                                        
                                Timer1 = timer.performWithDelay(2000,stopMovement, 1)
                                cando =false
                end
                        elseif ttouched == false then return false
        end     
end
 
thrustb:addEventListener ("touch", ttouch)
Runtime:addEventListener("enterFrame", ttap) 
 
end
 
                function thrustrButton()
                
        local thrustrb = display.newRect(0,0,50,50)
        thrustrb.x = _W/2  + thrustrb.contentWidth / 2 + 5
        thrustrb.y = _H - 50
        thrustrb:setFillColor(0, 0, 255)
        thrustrb.aplha = .75
        
        ttouched = false
        candor = true
        
local function trtouch(event)
         if event.phase == "began" then
        trtouched = true
        elseif event.phase == "ended" then
        trtouched = false
                candor = true
end
end
 
local function trtap(event)
        if trtouched == true then
                if candor == true then
                
                        vel = 50 -- this is the arbitrary amount of velocity to increase player motion by.
                        vely = -500
                        p1Rads = player1.rotation * math.pi / 180 -- step 1
 
                        local velX = math.cos (p1Rads) * vel -- upper case (velX/velx) to feed into obj:setLinearVelocity(X,Y)
                        local velY = math.sin (p1Rads) * vel
                        print(velX,velY,player1.rotation)
                                
                                
                                
                                player1:setLinearVelocity(velX,velY)
                                
                                        local function stopMovement()
                                                player1:setLinearVelocity(0,0)
                                        end
                                        
                                Timer1 = timer.performWithDelay(2000,stopMovement, 1)
                                candor =false
                end
                        elseif trtouched == false then return false
        end     
end
 
thrustrb:addEventListener ("touch", trtouch)
Runtime:addEventListener("enterFrame", trtap) 
 
end
 
 
lArrow()
rArrow()
thrustButton()
thrustrButton()
end
 
 
function startGame()
 
player()
buttons()
setplayerRot()
 
end
 
startGame()
views:2495 update:2012/2/13 9:11:28
corona forums © 2003-2011