Math is not my strong point, so if anyone can help, I would really much appreciate it. Here are the conditions:
- Object 1 travels in a constant speed going from top to bottom (let’s just say that its starting point is at X = 100, Y = 0)
- Sometime during object 1’s crossing of the screen, a 2nd object is launched from the bottom & middle of the screen (X = canvas.width/2, Y = canvas.height)
I’m sure that object 2 will either have to adjust its speed or curve at some point to hit object 1. I’m looking for a math formula where I can have object 2 hit object 1 before object 1 (same X, Y coordinates) reaches the bottom of the canvas. To motivate you or to make it fun, I suppose you can pretend that you are writing a formula to intercept incoming missiles for NASA or whoever…
I think this is a just a matter of looking for the distance between the two objects using a right-angle triangle a^2 + b ^2 = c^2 where c^2 eventually becomes 0 (or so)? As the two objects get closer to each other, the triangle will become smaller and smaller – resulting in c^2 being shorter and shorter. Something like that?
Thanks, but this is more of one object chasing another object. I’ll look at the link you provided to see if there is anything I can use.
You should take a look at the built-in Rect and Point classes. The Rect class has methods like Intersects and Contains that are very useful in 2D collision detection.
Dim MyRectangle As New REALbasic.Rect(5, 5, 10, 10) ' a 10x10 square, offset 5 pixels from top and left
Dim MyPoint As New REALbasic.Point(8, 8) ' a point at X,Y=8,8
If MyRectangle.Contains(MyPoint) Then
You want object 2 to continuously follow object 1? I was picturing object 1 as falling and object 2 as a cannon ball you wanted to fire that would collide with the falling ball.
And how is object 2 supposed to collide? I mean does it start moving when the falling object is half way down, or right away. Is object 2 going to move in a straight line or arc to object 2, is it’s motion constant or do you want some gravity effect?
I am looking to have object2 “fired” at some point as object1 is falling from top to bottom and have object2 meet object 1 at the same x,y coordinate. As such, obj1 x will never change, and it’s speed is constant. Obj2, depending on when fired, will need to either adjust is speed and/or curve to meet obj1 before obj1 gets to the bottom of the can as.
I don’t think collision detection is needed. I’m just looking to have obj1 meet up with obj2 before obj1 goes out of scope.
Again, obj2 can be fired off at anytime while obj1 is falling.
Thanks for all the help and suggestion so far.
Obj2 can be fired at any angle, or always vertically initially? Will Obj2’s speed be constant too?
Grrr, so hard to use this forum on iPads. I think my reply to Kem got deleted accidentally.
Obj2 can be fired In whichever angle obj1 is when obj2 is triggered. As for speed, it should not need to change, but I don’t know. Obj2 just has to meet obj1 along obj1’s x axis before obj1 goes out of scope.
Btw, both objects share the same timer for its crude animation.
Still not entirely clear, so let me try it this way. Let’s call Obj1 “target” and Obj2 “missile”. Target is “falling” along the y axis from a height of h at a known rate. Let’s say missile is fired when target is at exactly x,(h/2). Based on some yet-undermined formula, we know that missile can meet target at point a,b if it travels at its known speed in a straight line, so it aims at point a,b and takes off. But when it launches towards a,b, target is still above that point falling towards it. Is that what you want? Or…
Missile is launched when target is at x,(h/2). Missile must aim at that point since that’s where target is at the moment of launch, but adjust its course en route on an arc to intercept target at point a,b. Are these the rules?
Yes. Those are pretty much the rules. Missile can be fired at anytime during target’s fall. Missile just needs to hit target before target reach bottom of canvas.
Nona, I gave you two scenarios. Which are pretty much the rules? Either?
Either. Thanks Kem. Curving projectiles would look cool though
Well, the first is easier. Since target’s speed is constant, we know where it will be at any given point in time. Since missile’s speed is variable, when it’s fired, you can pick a random point between target’s current y and 0 (bottom) and aim for that. Since time = distance / speed, you can figure out how much time the missile will have to get to that point, the distance from the missile launch site to that point, then adjust the missile speed accordingly. All that’s left is the spectacular explosion when they meet.
Thanks Kem. I’ll have work on your solution first thing tomorrow. It’s getting quite late here in Minnesota.
Here’s a tidied version. Using doubles for smooth flight and paid attention to what I wrote Shows how to move towards a target at a constant speed using vectors. Look in resetButton for setup and theTimer for vector stepping.
For Kems idea I picture the missile traveling in a straight line to the exact intercept, but the math reveals a simultaneous equation I’m not sure how to solve. I think there’s a quadratic equation in there…
The missile is on the x axis at some coordinate X and the target is on the y axis falling at a constant ‘drop speed’ DS. When the missile is fired set time T = 0 and mark the targets current Y coordinate with H.
so then Y changes over time like this
Y = H - T * DS
For the missile it has a constant ‘missile speed’ MS. Over time it travels a distance of T * MS, and this distance needs to match the Y coordinate of the target at that time
T * MS = Sqrt(X^2 + Y^2)
H, DS, MS and X are known. Solve for Y or T to know the intercept.
[quote=33075:@stuart s]Something like this http://home.comcast.net/~trochoid/missile.xojo_binary_project.zip
It’s super crude. [/quote]
Perfect! Thank you, thank you.