How to code tank-turret like physics.

I am trying to code a game that includes a tank, representing the player, and have run into several road blocks.

  1. I am having trouble figuring out how to code the ‘turret’ portion of the tank.
    Do I use two separate images, one for the tank and one for the turret? or do I somehow isolate the turret portion of the image and animate that?

  2. Eventually when I resolve problem #1 how would I code it to where the user can control both the tank and the turret simultaneously? (like two ‘thumb-sticks’ one controlling the direction the tank is moving, and the other controlling the turret (which when released would cause the turret to shoot a projectile.)

Thanks in advance for any help! :slight_smile:

-Saer [import]uid: 148623 topic_id: 29858 reply_id: 329858[/import]

I would say two sprites - one for the lower body of the tank, one for the turret, so one can turn independently of the other, like tanks do.

As for controls - if the only thing the player can do is move the tank and turn the turret (and presumably fire the gun) then two circles, one bottom right, one bottom left, with the user using his thumbs to control the tank movement and direction on the left, let’s say, and the turret direction on the right, with tapping the turret firing it. You can even make those circles fairly translucent so that they don’t hide the action too much (and this way you can make them bigger to make it more convenient). [import]uid: 160496 topic_id: 29858 reply_id: 119722[/import]

I am fairly new to Corona (all I’ve been able to do so far is create a simple bubble-popper type game) so just to clarify when you say two separate sprites, do you mean two images or actual sprite sheets?

-Saer [import]uid: 148623 topic_id: 29858 reply_id: 119724[/import]

Two separate objects. Whether they are sprites (that is, objects created with display.newSprite if you’re using the new sprite interface or sprite.newSprite if you’re using the old one) or images depends on how you’re doing things. If you’re doing it in pseudo-3d then you’d have to use sprites for rotation, but if you’re doing a “view from the top”, you could get away with images that you rotate yourself. [import]uid: 160496 topic_id: 29858 reply_id: 119726[/import]

Awesome, thanks!
I have both images displayed, but when I try to change the rotation of the ‘turret’ it moves off of the tank-body-image instead of just rotating in-place.
How would I combine both images so the turret would remain stationary when the tank is moving, but still be able to rotate?

Also, about coding the ‘control sticks’ how would that look, syntax wise?
I know it’d probably have various “if” statements like, if touch event has occurred then that would cause the tank to move, with various parameters defined… I just can’t figure out the syntax to achieve this. :\

(Sorry for asking so many questions, I try to research and get answers for myself, but some of the descriptions are a little vague.)

-Saer [import]uid: 148623 topic_id: 29858 reply_id: 119815[/import]

It is hard to see without seeing the code and the images, but basically images rotate around the “reference” points. When you load the image, its reference point is set to its center, but you can change it.

Controls - basically, draw a circle, set the listener to it, respond to “moved” event.phase, check what the angle is between the event.x, event.y point and the center of the circle and rotate the corresponding objects (or move them in that direction) accordingly…

As for lua syntax - I would suggest reading the lua reference first. It would give you a good intro to the language itself. When I started with Corona, I went through that and that answered ALL kinds of questions I would have been asking later. http://www.lua.org/pil/ [import]uid: 160496 topic_id: 29858 reply_id: 119818[/import]

I would say two sprites - one for the lower body of the tank, one for the turret, so one can turn independently of the other, like tanks do.

As for controls - if the only thing the player can do is move the tank and turn the turret (and presumably fire the gun) then two circles, one bottom right, one bottom left, with the user using his thumbs to control the tank movement and direction on the left, let’s say, and the turret direction on the right, with tapping the turret firing it. You can even make those circles fairly translucent so that they don’t hide the action too much (and this way you can make them bigger to make it more convenient). [import]uid: 160496 topic_id: 29858 reply_id: 119722[/import]

I am fairly new to Corona (all I’ve been able to do so far is create a simple bubble-popper type game) so just to clarify when you say two separate sprites, do you mean two images or actual sprite sheets?

-Saer [import]uid: 148623 topic_id: 29858 reply_id: 119724[/import]

Two separate objects. Whether they are sprites (that is, objects created with display.newSprite if you’re using the new sprite interface or sprite.newSprite if you’re using the old one) or images depends on how you’re doing things. If you’re doing it in pseudo-3d then you’d have to use sprites for rotation, but if you’re doing a “view from the top”, you could get away with images that you rotate yourself. [import]uid: 160496 topic_id: 29858 reply_id: 119726[/import]

Awesome, thanks!
I have both images displayed, but when I try to change the rotation of the ‘turret’ it moves off of the tank-body-image instead of just rotating in-place.
How would I combine both images so the turret would remain stationary when the tank is moving, but still be able to rotate?

Also, about coding the ‘control sticks’ how would that look, syntax wise?
I know it’d probably have various “if” statements like, if touch event has occurred then that would cause the tank to move, with various parameters defined… I just can’t figure out the syntax to achieve this. :\

(Sorry for asking so many questions, I try to research and get answers for myself, but some of the descriptions are a little vague.)

-Saer [import]uid: 148623 topic_id: 29858 reply_id: 119815[/import]

It is hard to see without seeing the code and the images, but basically images rotate around the “reference” points. When you load the image, its reference point is set to its center, but you can change it.

Controls - basically, draw a circle, set the listener to it, respond to “moved” event.phase, check what the angle is between the event.x, event.y point and the center of the circle and rotate the corresponding objects (or move them in that direction) accordingly…

As for lua syntax - I would suggest reading the lua reference first. It would give you a good intro to the language itself. When I started with Corona, I went through that and that answered ALL kinds of questions I would have been asking later. http://www.lua.org/pil/ [import]uid: 160496 topic_id: 29858 reply_id: 119818[/import]