PDA

View Full Version : Solved: Detecting Shapes on Screen



baz
10-24-2010, 01:34 AM
Hi all,

Not sure if the `Title' may be misleading, but here's my question...
I came across the `moveme' PPT on pptalchemy -which I think is excellent, and is something I could use with my class of 10 year olds. I want to know, is there some vba that would stop the object from being moved? For example, if you are moving the arrow and came across a solid shape that this would act as an obstacle?

I've posted the link here for the `moveme'

http://www.pptalchemy.co.uk/powerpoint_vba_move_control.html

Hope this makes sense, and any help would be greatly appreciated.

Thanks in advance

Baz

lenourien
01-12-2011, 03:13 AM
Hello,
Actually you can see from the code that he is already doing that in some way.
He has a routine to check for the edges of the slide in the code for each arrow.
For instance in Sub goleft() he has:
If oshp.Left > 0 Then oshp.Left = oshp.Left - 10

In powerpoint the system of coordinates originate from the top left corner that's the point (0,0). Going to the right increases X (or oshp.left) and going down increases Y (or oshp.top). Similarly, the coordinates of a shape are the coordinates of that shape's top left corner.

So very simply, that piece of code means that if the left coordinate of the shape is bigger than 0 (not leaving the screen on the left side) then it is ok to move it 10pts to the left (oshape.left -10) with every click.

So lets say you have a shape called obstacle on the left of your moving shape.
your moving shape left side would come in contact with obstacle's right side. The coordinates would be obstacle.left + obstacle.width

so by reusing the previous formula
If oshp.Left > (obstacle.left + obstacle.width) Then oshp.Left = oshp.Left - 10

Of course you would have to add something to make sure the shape is really facing obstacles. that would be:

If ( (oshp.Left > obstacle.left + obstacle.width) or (oshp.top + oshp.height < obstacles.top) or (oshp.top > obstacle.top + obstacle.height) ) Then
{ oshp.Left = oshp.Left - 10}
What we did is say:
If:
1- oshp is further on the right than obstacle (no contact) OR
2- oshp is above obstacle (no contact) OR
3- oshp is below obstacle (no contact)
Then it is ok to move oshp 10pts to the left with each click

Like that you checked the horizontal and vertical position of your shape compared to obstacle position
Hope this helps.
Good luck

baz
01-15-2011, 03:00 AM
Hi lenourien

Thanks for the reply -I'd all but forgotten about it. I have followed you post -and inserted the lines, however the moving arrow doesn't detect the obstacle shape as and obstacle. When I've put in a msgbox for the coordinates for the obstacle shape -there is no return -so I guess somewhere down the line it isn't being recognised as an `obstacle'.
I could be missing something really obvious -any further thoughts?

Thanks for the help
Baz

John Wilson
01-16-2011, 06:10 AM
Hi Baz

This isn't that easy!

I wouldn't use an arrow for this. PPT sees all shapes as rectangular so the "invisible" parts of the arrow will trigger a collision

Create a custom function to detect "collisions" that can be used over and over.

Download the file below which shows you how to create a custom function that you can call to test collisions with several shapes. If it's for a school and you need help drop me an email john INSERT @ HERE PPTAlchemy.co.uk
http://www.pptalchemy.co.uk/Downloads/collision.ppt

baz
01-16-2011, 07:43 AM
Hi John

Once again, thanks for the help. The `collision' programme was exactly what I was after, I'm not sure exactly how I want to use it -probably to move shapes (maybe with multiplication tables) to correct places, within a given time without colliding with walls etc. But I think it will probably open up lots of new possibilities.

Thanks again, and for the offer of dropping you a line `when' (as opposed to `if') I get stuck.

Thanks


Baz