How to hit a curved obstacle correctly

I am relatively new to Corona and have a question about best way to solve this problem:

I have a bullet that is moving towards a curved object (semi-circular) and when the bullet hits I want it to reflect in the opposite direction so if the tip of the bullet hits with an x,y velocity it may leave with a x,-y (for example) velocity but the bullet would be facing the new direction as well.

Is there an example like this? When I tried doing this using the physics engine I ended up with the bullet spinning and not point the right way. [import]uid: 111413 topic_id: 24484 reply_id: 324484[/import]

I believe that Rum (available in code exchange, provided by Graham Ranson) may help with setting the rotation correctly.

Have you got the curve set up properly on the other object? (I ask as you say you are new and physics can be tricky at first.)

For the bullet if you plan on manually rotating it (possibly using Rum) then you could set it to obj.isFixedRotation = true and it will not spin :slight_smile: [import]uid: 52491 topic_id: 24484 reply_id: 99172[/import]

I used physics editor to create the shapes and the bounce seems fine. The issue is I need the “bullet” to face the direction it is going. So the front should face the target but when the bullet hits the target it should bounce away and the front should rotate to the direction it is going after bounce.

I’m wondering what the correct technique is to rate the bullet to the correct direction so it follows the bounce.

rum has some useful methods but didn’t see this particular case. [import]uid: 111413 topic_id: 24484 reply_id: 99283[/import]

A simple solution to this (I do something similar in my game) is to execute a very fast timer upon collision, perhaps 50 milliseconds. When the timer fires, you read the bullet’s new X and Y velocity (coming off the bounce) and set the rotation of the bullet using those parameters and a basic “velocity to angle” trig math calculation. The user will not notice the fast shift, and typically 50 milliseconds should be enough for your bullet to begin its new directional path, thus the rotation will match.

Also, as Peach says, remember to set your bullet to “.isFixedRotation = true” to prevent the spinning motion.

Brent [import]uid: 9747 topic_id: 24484 reply_id: 99299[/import]

Sounds good, how do I get the x/y velocity? I don’t see this in the object. [import]uid: 111413 topic_id: 24484 reply_id: 99401[/import]

Here you go: the functions from my game… this should accomplish what you need. :slight_smile:

Brent

[code]
local math_deg, math_atan2 = math.deg, math.atan2

local function angleBetween( srcX, srcY, dstX, dstY )
local angle = ( math_deg( math_atan2( dstY-srcY, dstX-srcX ) )+90 )
return angle
end

local function rotateAfter( event )
timer.cancel( event.source ) ; event.source = nil --cancel timer (optional)
local vx,vy = bullet:getLinearVelocity() ; local pr = angleBetween( 0, 0, vx, vy )
bullet.rotation = pr
end

–after bullet collision, start timer
local t = timer.performWithDelay( 50, rotateAfter )
[/code] [import]uid: 9747 topic_id: 24484 reply_id: 99426[/import]

The angles aren’t exactly right but from here I can figure it out. I’m wondering if I am using the wrong src/dst coords for calculating the angle?

I was using the center of the screen and the object x/y velocity should I be using a different src x/y?
[import]uid: 111413 topic_id: 24484 reply_id: 99489[/import]

( 0, 0 ) should be correct for the “source” X/Y in this case, because you’re just calculating the direction of the bullet in relation to the top-left of the screen. Remember that coordinate 0,0 is the *top-left* point of any display GROUP, while 0,0 is (by default) the *center* of any display OBJECT. That’s a slightly odd aspect of Corona which I still sometimes have to ponder.

Also remember that your bullet image should be drawn so it’s pointing UP, not to the right, because rotation of 0 in Corona is straight up. The angle calculation will then rotate it properly to the direction you need, after the bounce. Of course, if your bullet is starting out on a 90-degree directional path, you’ll need to rotate it to that angle to begin with.
[import]uid: 9747 topic_id: 24484 reply_id: 99525[/import]

Ok, I figured it out. I don’t know what I was thinking but I was using the wrong coord to calculate the angle from.
Works fine now. Thank you. [import]uid: 111413 topic_id: 24484 reply_id: 99527[/import]